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