Regex Flashcards
Ruby Regexp
match any character
.
Ruby Regexp
escape regex special characters
\
Ruby Regexp
character class delimiter
[]
ex, would look to match any 1 of the set [aeiou] [A-Z] [a-z]
Ruby Regexp
create a range within a character class
-
ex [a-z] [A-Z] [0-9]
Ruby Regexp
invert the set of characters in a class, i.e. ‘not’
ex [^0-9] [^a-z]
Ruby Regexp
anchor that matches the position before the first character of the line
\A or ^
in ruby \A matches the start of the entire string, compared to ^ which will match the start of any line
Ruby Regexp
anchor that matches the position after the last character of the string
\z or $
in ruby \z matches the end of the entire string, compared to $ which will match the end of any line
Ruby Regexp
anchor that ensures that the following characters match pattern
, but doesn’t include those characters in the matched text
(?=pattern)
positive lookahead
Ruby Regexp
anchor that ensures that the preceding characters match pattern
, but does not include those characters in the matched text
(?<=pattern)
positive lookbehind
Ruby Regexp
anchor that ensures that the following characters do not match pattern
but doesn’t include them in the matched text
(?!pattern)
negative lookahead
Ruby Regexp
anchor that ensures the preceding characters do not match pattern
, but doesn’t include those characters in the matched text
(?< !pattern)
negative lookbehind
Ruby Regexp
quantifier that makes the previous character optional
?
Ruby Regexp
quantifier that specifies one or more of the previous character
+
Ruby Regexp
quantifier that specifies zero or more of the previous character
*
Ruby Regexp
quantifier that specifies a minimum and maximum number of times the previous character can be repeated
{min,max}
ex
{2,4} # 2 to 4 times
{0,1} # same as ?
{1,} # same as +
{0,} # same as *
{3} # exactly 3 times
Ruby Regexp
makes a repetition quantifier lazy instead of greedy
?
Ruby Regexp
sequence that escapes a string of characters, matching them as literal characters
\Q…\E
Ruby Regexp
alternator symbol in regex
|
Ruby Regexp
metacharacter matching a position called ‘word boundary’
\b
Ruby Regexp
subtract one characters class from another
[set-[subtract]]
ex [a-z-[aeiou]]
Ruby Regexp
with negation and subtraction in a character class, this takes precedence
negation
Ruby Regexp
used for grouping / capturing part of a regular expression
(…)
Ruby Regexp
used within parentheses so that a group is not captured
?:
ex color=(?:red|green|blue)
Ruby Regexp
names a captured group
(?< pattern>)
ex
$(?\d+).(?\d+)
Ruby Regexp
uses a named capture later in the regular expression
\k
ex
/(?[aeiou]).\k.\k/
Ruby Regexp
shorthand for [0-9]
\d
Ruby Regexp
shorthand for [A-Za-z0-9]
\w
Ruby Regexp
shorthand for [\t\r\n\f]
\s # ex space, a tab, a line break, or a form feed, and others depending on flavor of regex
Ruby Regexp
shorthand for [^\d]
\D
Ruby Regexp
shorthand for [^\w]
\W
Ruby Regexp
shorthand for [^\s]
\S
Ruby Regexp
regex delimiter in ruby
/expression/
Ruby Regexp
regex method that takes a string as an argument and returns the index of the first match in string
Regexp#=~
Ruby Regexp
regex / string method that returns a MatchData object
Regexp#match
String#match
Ruby Regexp
in a MatchData object, returns the original string that was evaluated
ex
MatchData#string
m.string
Ruby Regexp
in a MatchData object, returns the substring that matched
ex
MatchData#[0]
m[0]
Ruby Regexp
in a MatchData object, returns the nth capture
ex
MatchData#[n]
m[n]
Ruby Regexp
string method that takes a regex as an argument and returns an array of substrings delimited by the regex matches (i.e. discards the matches returning an array of each remaining substring)
String#split