

Regex examples full#
# Nesting of capture groups, extract full name, and both parts. "regex in Ruby" =~ /\bRuby\b/" # match the word "Ruby", but not "Ruby" as larger string "Ruby" =~ /R\wy/ # match one word character "Rbuy" =~ /*/ # match from character set R,u,b and y "Ruby" =~ /Ry/ # match from character range a-z "Ruby" =~ /R.*y/ # match multipe characters Matching with wildcards "Ruby" =~ /R.y/ # match a single character with. Text =~ /ruby/i # case in-sensitive string matching " =~ %r# match protocol prefix with # delimiterĬase sensitity text =~ /Ruby/ # case sensitive string matching Using different regex delimiters %r/Ruby/ # / as commonly used delimiterĬhanging the delimiter becomes useful in some cases " =~ /http:\/\// # match protocol prefix with / delimiter Text =~ /^Ruby$/ # Match for exact string content A character set refers to a regular expression that returns any matches that align with a defined. const re / w + s / g const str fee fi fo fum const myArray str. A flag is a modifier that allows you to define your matched results. Text =~ /Ruby$/ # Match literal at end of string For example, re /w+s/g creates a regular expression that looks for one or more characters followed by a space, and it looks for this combination throughout the string. Text =~ /^Ruby/ # Match literal at start of string Here are some syntax examples that check strings for certain content: Basic Matching text =~ /Ruby/ # Match for an unbound literal So you can just match something with putting a regular expression between two slashes: /search string/ 2. SyntaxĬompared to other scripting languages Ruby more behaves like Perl allowing to use regex seemlessly without the need for explicit objects in Python or a function in PHP.

Some simple examples for using regular expressions in Ruby scripts.
