-
=~
Returns the index of the first match -
[]
return the first match -
gsub
Takes a regex expression, and a second argument. Replaces all instances of the target (first argument) with the second argument.a = "Hello, its me!" a.gsub(/e/, "pizza") #=> "Hpizzallo, it's mpizza!" a.gsub(/l/, "pizza") #=> "Hepizzapizzao, it's me!"
-
split
Splits string based on the character argumert provided (a.split(/<character(s)>/
). Removes the arg character(s) from the string, returns an array of stringsa = "Hello, its me!" a.split # => ["Hello,", "its", "me!"] a.split(/i/) # => ["Hello, ", "ts me!"] a.split(/ll/) # => ["He", "o, its me!"] a.split(/z/) # => ["Hello, its me!"]
-
scan
Returns an array of matches
-
/P/
A character, anywhere in the string -
/PM/
Two characters next to each other -
/M+/
One or more of its target -
/M*/
Zero or more of its target -
/M?/
Zero or one of its target"b ba baa baaa baaaaa".scan(/ba?/) # => ["b", "ba", "ba", "ba", "ba"]
vs.
"b ba baa baaa baaaaa".scan(/ba*/) # => ["b", "ba", "baa", "baaa", "baaaaa"]
-
/./
match any character -
/^M/
matches its target if target exists at the beginning of a line in a stringstr = "Regexes are badass.\nI wonder if strings are jealous?\nRegexes are king." str.scan(/^Regexes/) # => ["Regexes", "Regexes"]
-
a{3,}
3 or more of 'a' so in 'haaapppy go lucky bunny rabbit' this will select 'aaa', but in 'haaaapppy go lucky bunny rabbit' you will get 'aaaa'. you can change this number to select whatever you want, however if you use 1 as in => a{1,} this will select every combination of 'a' so 'happpy go lucky bunny raaaabbit1' => 'a','aaaa' -
\z
Matches targets (".." in example) starting from the end of the string and working backwards from there."Joshua Mejia [7:58 PM]"[(/..\z/]) => "M]"
vs.
"Joshua Mejia [7:58 PM]"[(/...\z/]) => "PM]"
-
\s
Any whitespace character If using scan, will identify the character before or after. For example, string = "whatup yo". If you enter this: string.scan(/p\s/), you will get ["p "]. Does not account for more than one empty space. -
\W
Any non-word character: Finds spaces, commas, colons, etc - things that wouldn't be part of a word. This does find apostraphe's also, so look out for contractions! -
(...)
Capture an item in the string and then use.
to represent any place your want around the string -
\w
Any word character (letter, number, underscore)"Joshua Mejia [7:58 PM]".scan(/\W/) => [" ", " ", "[", ":", " ", "]"]
"Joshua Mejia [7:58 PM]".scan(/.\W/) => ["a ", "a ", "7:", "8 ", "M]"]
-
\b
Any word boundary -
()
group items together. Ruby will also "capture" what gets matched and let you access it with$1
,$2
, ..."HTTP/1.1 200 OK"[/(\S+) (\S+) (.+)/] # => "HTTP/1.1 200 OK" $1 # => "HTTP/1.1" $2 # => "200" $3 # => "OK"
-
a{3,6}
Between 3 and 6 of a"hellllo worllllld!".scan(l{3,6}) => ["llll", "lllll"]
If there are more than the max, the regex grabs the maximum, leaving behind the remainder of the characters as such:
"777777777".scan(7{3-6}) => ["777777", "777"]
-
The positive lookahead is designated by regex_to_match(?=regex_after). It returns a match when regex_to_match is followed by regex_after. The match does not return the regex searched for within the parentheses. For example:
"aXbXcXdY".scan(/.(?=X)/) #=> ["a", "b", "c"]
"aX bY c#4".scan(/.(?=\w)/) #=> ["a", "b"]
- The negative lookahead is designated by regex_to_match(?!regex_after). It returns a match when regex_to_match is not followed by regex_after. Similar to the positive lookahead, the match does not return the regex searched for within the parentheses.
"aXbXcXdY".scan(/[a-d](?!X)/) #=> ["d"]
"aX bY c#4d5".scan(/[a-c](?!\w)/) #=> ["c"]
[abc]
A single character of: a, b, or c[^abc]
Any single character except: a, b, or c `"aabbfbezabeas abrba acaoboalb".scan(/[^ab]/) => ["f", "e", "z", "e", "s", " ", "r", " ", "c", "o", "o", "l"][a-z]
Any single character in the range a-z[a-zA-Z]
Any single character in the range a-z or A-Z^
Start of line$
End of line\A
Start of string\S
Any non-whitespace character\d
Any digitstr.scan(/\d/)
found only the digits 0-9 in the string\D
Any non-digitstr.scan(/\D/)
found only the nondigits a-z in the string(a|b)
a or ba?
Zero or one of aa*
Zero or more of aa+
One or more of aa{3}
Exactly 3 of aa{3,}
3 or more of a