Dot metacharacter and Quantifiers
This chapter introduces dot metacharacter and quantifiers. As the name implies, quantifiers allows you to specify how many times a character or grouping should be matched. With *
string operator, you can do something like 'no' * 5
to get "nonononono"
. This saves you manual repetition as well as gives the ability to programmatically repeat a string object as many times as you need. Quantifiers support this simple repetition as well as ways to specify a range of repetition. This range has the flexibility of being bounded or unbounded with respect to start and end values. Combined with dot metacharacter (and alternation if needed), quantifiers allow you to construct conditional AND logic between patterns.
Dot metacharacter
The dot metacharacter matches any character except the newline character.
# matches character 'c', any character and then character 't'
>> 'tac tin c.t abc;tuv acute'.gsub(/c.t/, 'X')
=> "taXin X abXuv aXe"
# matches character 'r', any two characters and then character 'd'
>> 'breadth markedly reported overrides'.gsub(/r..d/) { |s| s.upcase }
=> "bREADth maRKEDly repoRTED oveRRIDes"
# matches character '2', any character and then character '3'
>> "42\t33".sub(/2.3/, '8')
=> "483"
See m modifier section to know how .
can match newline as well. Chapter Character class will discuss how to define your own custom placeholder for limited set of characters.
split method
This chapter will additionally use split
method to illustrate examples. The split
method separates the string based on given regexp (or string) and returns an array of strings.
# same as: 'apple-85-mango-70'.split('-')
>> 'apple-85-mango-70'.split(/-/)
=> ["apple", "85", "mango", "70"]
>> 'bus:3:car:5:van'.split(/:.:/)
=> ["bus", "car", "van"]
# optional limit can be specified as second argument
# when limit is positive, you get maximum of limit-1 splits
>> 'apple-85-mango-70'.split(/-/, 2)
=> ["apple", "85-mango-70"]
See split with capture groups section for details of how capture groups affect the output of split
method.
Greedy quantifiers
Quantifiers have functionality like the string repetition operator and range method. They can be applied to both characters and groupings (and more, as you'll see in later chapters). Apart from ability to specify exact quantity and bounded range, these can also match unbounded varying quantities. If the input string can satisfy a pattern with varying quantities in multiple ways, you can choose among three types of quantifiers to narrow down possibilities. In this section, greedy type of quantifiers is covered.
First up, the ?
metacharacter which quantifies a character or group to match 0
or 1
times. In other words, you make that character or group as something to be optionally matched. This leads to a terser regexp compared to alternation and grouping.
# same as: /ear|ar/
>> 'far feat flare fear'.gsub(/e?ar/, 'X')
=> "fX feat flXe fX"
# same as: /\bpar(t|)\b/
>> 'par spare part party'.gsub(/\bpart?\b/, 'X')
=> "X spare X party"
# same as: /\b(re.d|red)\b/
>> words = %w[red read ready re;d road redo reed rod]
>> words.grep(/\bre.?d\b/)
=> ["red", "read", "re;d", "reed"]
# same as: /part|parrot/
>> 'par part parrot parent'.gsub(/par(ro)?t/, 'X')
=> "par X X parent"
# same as: /part|parrot|parent/
>> 'par part parrot parent'.gsub(/par(en|ro)?t/, 'X')
=> "par X X X"
The *
metacharacter quantifies a character or group to match 0
or more times. There is no upper bound, more details will be discussed later in this chapter.
# match 't' followed by zero or more of 'a' followed by 'r'
>> 'tr tear tare steer sitaara'.gsub(/ta*r/, 'X')
=> "X tear Xe steer siXa"
# match 't' followed by zero or more of 'e' or 'a' followed by 'r'
>> 'tr tear tare steer sitaara'.gsub(/t(e|a)*r/, 'X')
=> "X X Xe sX siXa"
# match zero or more of '1' followed by '2'
>> '3111111111125111142'.gsub(/1*2/, 'X')
=> "3X511114X"
Here's some examples with split
and related methods. partition
splits the input string on the first match and the text matched by the regexp is also present in the output. rpartition
is like partition
but splits on the last match.
# note how '25' and '42' gets split, there is '1' zero times in between them
>> '3111111111125111142'.split(/1*/)
=> ["3", "2", "5", "4", "2"]
# there is '1' zero times at end of string as well, note the use of -1 for limit
>> '3111111111125111142'.split(/1*/, -1)
=> ["3", "2", "5", "4", "2", ""]
>> '3111111111125111142'.partition(/1*2/)
=> ["3", "11111111112", "5111142"]
# last element is empty because there is nothing after 2 at the end of string
>> '3111111111125111142'.rpartition(/1*2/)
=> ["311111111112511114", "2", ""]
The +
metacharacter quantifies a character or group to match 1
or more times. Similar to *
quantifier, there is no upper bound. More importantly, this doesn't have surprises like matching empty string in between patterns or at the end of string.
>> 'tr tear tare steer sitaara'.gsub(/ta+r/, 'X')
=> "tr tear Xe steer siXa"
>> 'tr tear tare steer sitaara'.gsub(/t(e|a)+r/, 'X')
=> "tr X Xe sX siXa"
>> '3111111111125111142'.gsub(/1+2/, 'X')
=> "3X5111142"
>> '3111111111125111142'.split(/1+/)
=> ["3", "25", "42"]
You can specify a range of integer numbers, both bounded and unbounded, using {}
metacharacters. There are four ways to use this quantifier as listed below:
Pattern | Description |
---|---|
{m,n} | match m to n times |
{m,} | match at least m times |
{,n} | match up to n times (including 0 times) |
{n} | match exactly n times |
>> demo = %w[abc ac adc abbc xabbbcz bbb bc abbbbbc]
>> demo.grep(/ab{1,4}c/)
=> ["abc", "abbc", "xabbbcz"]
>> demo.grep(/ab{3,}c/)
=> ["xabbbcz", "abbbbbc"]
>> demo.grep(/ab{,2}c/)
=> ["abc", "ac", "abbc"]
>> demo.grep(/ab{3}c/)
=> ["xabbbcz"]
The
{}
metacharacters have to be escaped to match them literally. However, unlike()
metacharacters, these have lot more leeway. For example, escaping{
alone is enough, or if it doesn't conform strictly to any of the four forms listed above, escaping is not needed at all. Also, if you are applying{}
quantifier to#
character, you need to escape the#
to override interpolation.
AND conditional
Next up, how to construct AND conditional using dot metacharacter and quantifiers.
# match 'Error' followed by zero or more characters followed by 'valid'
>> 'Error: not a valid input'.match?(/Error.*valid/)
=> true
>> 'Error: key not found'.match?(/Error.*valid/)
=> false
To allow matching in any order, you'll have to bring in alternation as well. That is somewhat manageable for 2 or 3 patterns. See AND conditional with lookarounds section for an easier approach.
>> seq1, seq2 = ['cat and dog', 'dog and cat']
>> seq1.match?(/cat.*dog|dog.*cat/)
=> true
>> seq2.match?(/cat.*dog|dog.*cat/)
=> true
# if you just need true/false result, this would be a scalable approach
>> patterns = [/cat/, /dog/]
>> patterns.all? { |re| seq1.match?(re) }
=> true
>> patterns.all? { |re| seq2.match?(re) }
=> true
What does greedy mean?
When you are using the ?
quantifier, how does Ruby decide to match 0
or 1
times, if both quantities can satisfy the regexp? For example, consider this substitution expression 'foot'.sub(/f.?o/, 'X')
— should foo
be replaced or fo
? It will always replace foo
because these are greedy quantifiers, meaning they try to match as much as possible.
>> 'foot'.sub(/f.?o/, 'X')
=> "Xt"
# a more practical example
# prefix '<' with '\' if it is not already prefixed
# both '<' and '\<' will get replaced with '\<'
>> puts 'blah \< foo < bar \< blah < baz'.gsub(/\\?</, '\<')
blah \< foo \< bar \< blah \< baz
# say goodbye to /handful|handy|hand/ shenanigans
>> 'hand handy handful'.gsub(/hand(y|ful)?/, 'X')
=> "X X X"
But wait, then how did /Error.*valid/
example work? Shouldn't .*
consume all the characters after Error
? Good question. The regular expression engine actually does consume all the characters. Then realizing that the regexp fails, it gives back one character from end of string and checks again if the overall regexp is satisfied. This process is repeated until a match is found or failure is confirmed. In regular expression parlance, this is called backtracking.
>> sentence = 'that is quite a fabricated tale'
# /t.*a/ will always match from first 't' to last 'a'
# also, note that 'sub' is being used here, not 'gsub'
>> sentence.sub(/t.*a/, 'X')
=> "Xle"
>> 'star'.sub(/t.*a/, 'X')
=> "sXr"
# matching first 't' to last 'a' for t.*a won't work for these cases
# the regexp engine backtracks until .*q matches and so on
>> sentence.sub(/t.*a.*q.*f/, 'X')
=> "Xabricated tale"
>> sentence.sub(/t.*a.*u/, 'X')
=> "Xite a fabricated tale"
Backtracking can be quite time consuming for certain corner cases (see ruby-doc: Regexp Performance). Or even catastrophic (see cloudflare: Details of the Cloudflare outage on July 2, 2019).
Non-greedy quantifiers
As the name implies, these quantifiers will try to match as minimally as possible. Also known as lazy or reluctant quantifiers. Appending a ?
to greedy quantifiers makes them non-greedy.
>> 'foot'.sub(/f.??o/, 'X')
=> "Xot"
>> 'frost'.sub(/f.??o/, 'X')
=> "Xst"
>> '123456789'.sub(/.{2,5}?/, 'X')
=> "X3456789"
>> 'green:3.14:teal::brown:oh!:blue'.split(/:.*?:/)
=> ["green", "teal", "brown", "blue"]
Like greedy quantifiers, lazy quantifiers will try to satisfy the overall regexp.
>> sentence = 'that is quite a fabricated tale'
# /t.*?a/ will always match from first 't' to first 'a'
>> sentence.sub(/t.*?a/, 'X')
=> "Xt is quite a fabricated tale"
# matching first 't' to first 'a' for t.*?a won't work for this case
# so, regexp engine will move forward until .*?f matches and so on
>> sentence.sub(/t.*?a.*?f/, 'X')
=> "Xabricated tale"
# this matches last 'e' after 'q' to satisfy the anchor requirement
>> sentence.sub(/q.*?e$/, 'X')
=> "that is X"
Possessive quantifiers
Appending a +
to greedy quantifiers makes them possessive quantifiers. These are like greedy quantifiers, but without the backtracking. So, something like /Error.*+valid/
will never match because .*+
will consume all the remaining characters. If both the greedy and possessive quantifier versions are functionally equivalent, then possessive is preferred because it will fail faster for non-matching cases.
# functionally equivalent greedy and possessive versions
>> %w[abc ac adc abbc xabbbcz bbb bc abbbbbc].grep(/ab*c/)
=> ["abc", "ac", "abbc", "xabbbcz", "abbbbbc"]
>> %w[abc ac adc abbc xabbbcz bbb bc abbbbbc].grep(/ab*+c/)
=> ["abc", "ac", "abbc", "xabbbcz", "abbbbbc"]
# different results
# numbers >= 100 if there are leading zeros
# \d will be discussed in a later chapter, it matches all digit characters
>> '0501 035 154 12 26 98234'.gsub(/\b0*\d{3,}\b/, 'X')
=> "X X X 12 26 X"
>> '0501 035 154 12 26 98234'.gsub(/\b0*+\d{3,}\b/, 'X')
=> "X 035 X 12 26 X"
The effect of possessive quantifier can also be expressed using atomic grouping. The syntax is (?>pat)
, where pat
is an abbreviation for a portion of regular expression pattern. In later chapters you'll see more such special groupings.
# same as: /(b|o)++/
>> 'abbbc foooooot'.gsub(/(?>(b|o)+)/, 'X')
=> "aXc fXt"
# same as: /\b0*+\d{3,}\b/
>> '0501 035 154 12 26 98234'.gsub(/\b(?>0*)\d{3,}\b/, 'X')
=> "X 035 X 12 26 X"
Cheatsheet and Summary
Note | Description |
---|---|
. | match any character except the newline character |
greedy | match as much as possible |
? | greedy quantifier, match 0 or 1 times |
* | greedy quantifier, match 0 or more times |
+ | greedy quantifier, match 1 or more times |
{m,n} | greedy quantifier, match m to n times |
{m,} | greedy quantifier, match at least m times |
{,n} | greedy quantifier, match up to n times (including 0 times) |
{n} | greedy quantifier, match exactly n times |
pat1.*pat2 | any number of characters between pat1 and pat2 |
pat1.*pat2|pat2.*pat1 | match both pat1 and pat2 in any order |
non-greedy | append ? to greedy quantifier |
match as minimally as possible | |
possessive | append + to greedy quantifier |
like greedy, but no backtracking | |
(?>pat) | atomic grouping, similar to possessive quantifier |
s.split(/pat/) | split a string based on pat |
accepts an optional limit argument to control no. of splits | |
s.partition(/pat/) | returns array of 3 elements based on first match of pat |
portion before match, matched portion, portion after match | |
s.rpartition(/pat/) | returns array of 3 elements based on last match of pat |
This chapter introduced the concept of specifying a placeholder instead of fixed string. When combined with quantifiers, you've seen a glimpse of how a simple regexp can match wide range of text. In coming chapters, you'll learn how to create your own restricted set of placeholder characters.
Exercises
Since
.
metacharacter doesn't match newline character by default, assume that the input strings in the following exercises will not contain newline characters.
a) Replace 42//5
or 42/5
with 8
for the given input.
>> ip = 'a+42//5-c pressure*3+42/5-14256'
>> ip.gsub() ##### add your solution here
=> "a+8-c pressure*3+8-14256"
b) For the array items
, filter all elements starting with hand
and ending with at most one more character or le
.
>> items = %w[handed hand handled handy unhand hands handle]
>> items.grep() ##### add your solution here
=> ["hand", "handy", "hands", "handle"]
c) Use split
method to get the output as shown for the given input strings.
>> eqn1 = 'a+42//5-c'
>> eqn2 = 'pressure*3+42/5-14256'
>> eqn3 = 'r*42-5/3+42///5-42/53+a'
>> pat = ##### add your solution here
>> eqn1.split(pat)
=> ["a+", "-c"]
>> eqn2.split(pat)
=> ["pressure*3+", "-14256"]
>> eqn3.split(pat)
=> ["r*42-5/3+42///5-", "3+a"]
d) For the given input strings, remove everything from the first occurrence of i
till end of the string.
>> s1 = 'remove the special meaning of such constructs'
>> s2 = 'characters while constructing'
>> pat = ##### add your solution here
>> s1.sub(pat, '')
=> "remove the spec"
>> s2.sub(pat, '')
=> "characters wh"
e) For the given strings, construct a regexp to get output as shown.
>> str1 = 'a+b(addition)'
>> str2 = 'a/b(division) + c%d(#modulo)'
>> str3 = 'Hi there(greeting). Nice day(a(b)'
>> remove_parentheses = ##### add your solution here
>> str1.gsub(remove_parentheses, '')
=> "a+b"
>> str2.gsub(remove_parentheses, '')
=> "a/b + c%d"
>> str3.gsub(remove_parentheses, '')
=> "Hi there. Nice day"
f) Correct the given regexp to get the expected output.
>> words = 'plink incoming tint winter in caution sentient'
# wrong output
>> change = /int|in|ion|ing|inco|inter|ink/
>> words.gsub(change, 'X')
=> "plXk XcomXg tX wXer X cautX sentient"
# expected output
>> change = ##### add your solution here
>> words.gsub(change, 'X')
=> "plX XmX tX wX X cautX sentient"
g) For the given greedy quantifiers, what would be the equivalent form using {m,n}
representation?
?
is same as*
is same as+
is same as
h) (a*|b*)
is same as (a|b)*
— true or false?
i) For the given input strings, remove everything from the first occurrence of test
(irrespective of case) till end of the string, provided test
isn't at the end of the string.
>> s1 = 'this is a Test'
>> s2 = 'always test your RE for corner cases'
>> s3 = 'a TEST of skill tests?'
>> pat = ##### add your solution here
>> s1.sub(pat, '')
=> "this is a Test"
>> s2.sub(pat, '')
=> "always "
>> s3.sub(pat, '')
=> "a "
j) For the input array words
, filter all elements starting with s
and containing e
and t
in any order.
>> words = %w[sequoia subtle exhibit asset sets tests site]
>> words.grep() ##### add your solution here
=> ["subtle", "sets", "site"]
k) For the input array words
, remove all elements having less than 6
characters.
>> words = %w[sequoia subtle exhibit asset sets tests site]
>> words.grep() ##### add your solution here
=> ["sequoia", "subtle", "exhibit"]
l) For the input array words
, filter all elements starting with s
or t
and having a maximum of 6
characters.
>> words = %w[sequoia subtle exhibit asset sets tests site]
>> words.grep() ##### add your solution here
=> ["subtle", "sets", "tests", "site"]
m) Can you reason out why this code results in the output shown? The aim was to remove all <characters>
patterns but not the <>
ones. The expected result was 'a 1<> b 2<> c'
.
>> ip = 'a<apple> 1<> b<bye> 2<> c<cat>'
>> ip.gsub(/<.+?>/, '')
=> "a 1 2"
n) Use split
method to get the output as shown below for given input strings.
>> s1 = 'go there :: this :: that'
>> s2 = 'a::b :: c::d e::f :: 4::5'
>> s3 = '42:: hi::bye::see :: carefully'
>> pat = ##### add your solution here
>> s1.split(pat, 2)
=> ["go there", "this :: that"]
>> s2.split(pat, 2)
=> ["a::b", "c::d e::f :: 4::5"]
>> s3.split(pat, 2)
=> ["42:: hi::bye::see", "carefully"]
o) For the given input strings, match if the string starts with optional space characters followed by at least two #
characters.
>> s1 = ' ## header2'
>> s2 = '#### header4'
>> s3 = '# comment'
>> s4 = 'normal string'
>> s5 = 'nope ## not this'
>> pat = ##### add your solution here
>> s1.match?(pat)
=> true
>> s2.match?(pat)
=> true
>> s3.match?(pat)
=> false
>> s4.match?(pat)
=> false
>> s5.match?(pat)
=> false