Quantifiers
Quantifiers specify the number of instances of a character that must be present in the input for a match to occur.
Yellowbrick supports both greedy and lazy quantifiers. Quantifiers are greedy by default and will try to match as many characters as possible. Alternatively, lazy quantifiers will try to match an element as few times as possible.
The following quantifiers are supported:
Quantifier | Matches |
---|---|
* | Sequence of 0 or more matches |
*? | Lazy version of * |
+ | Sequence of 1 or more matches |
+? | Lazy version of + |
? | Sequence of 0 or 1 matches |
?? | Lazy version of ? |
{m} | Sequence of exactly m matches |
{m}? | Lazy version of {m} |
{m,} | Sequence of m or more matches |
{m,}? | Lazy version of {m,} |
{m,n} | Sequence of m through n (inclusive) matches where m cannot exceed n |
{m,n}? | Lazy version of {m,n} |
Note: Other regular expression engines apply the behavior of the first quantifier to all subsequent quantifiers in the expression. The Yellowbrick engine treats all quantifiers in the expression by their indicated behavior. For example, the Yellowbrick engine would evaluate a+?b*
as a lazy quantifier followed by a greedy quantifier instead of treating both quantifiers as lazy.
Quantifier Examples
The ??
quantifier applies to the preceding character e
. The string Barnsley
matches because the pattern looks for 0 or 1 occurrences of e
in that position in the string.
premdb=# select * from team where name ~ 'Barne??sley';
teamid | htid | atid | name | nickname | city | stadium | capacity | avg_att
--------+------+------+----------+----------+----------+-----------------+----------+---------
3 | 4 | 53 | Barnsley | Tykes | Barnsley | Oakwell Stadium | 23009 | 0.000
(1 row)
The {9,15}?
quantifier in this example looks for a sequence of between 9 and 15 characters anywhere in the string. The row with teamid 43
qualifies because Hawthorns
is a 9-character string.
premdb=# select * from team where stadium ~ '\w{9,15}?';
teamid | htid | atid | name | nickname | city | stadium | capacity | avg_att
--------+------+------+----------------------+-----------+---------------+----------------------+----------+---------
6 | 7 | 56 | Blackpool | Seasiders | Blackpool | Bloomfield Road | 17338 | 0.000
26 | 27 | 76 | Middlesbrough | Boro | Middlesbrough | Riverside Stadium | 34742 | 0.000
35 | 36 | 85 | Sheffield Wednesday | Owls | Sheffield | Hillsborough Stadium | 39732 | 0.000
37 | 38 | 87 | Stoke City | Potters | Stoke | Britannia Stadium | 27902 | 27.534
43 | 44 | 93 | West Bromwich Albion | Baggies | Birmingham | The Hawthorns | 27000 | 24.631
(5 rows)
Again, the {9,15}?
quantifier in this example looks for a sequence of between 9 and 15 characters anywhere in the string. However, the pattern only matches if a space character (\s
) follows the string of alphanumeric characters.
premdb=# select * from team where stadium ~ '\w{9,15}?\s';
teamid | htid | atid | name | nickname | city | stadium | capacity | avg_att
--------+------+------+---------------------+-----------+---------------+----------------------+----------+---------
6 | 7 | 56 | Blackpool | Seasiders | Blackpool | Bloomfield Road | 17338 | 0.000
26 | 27 | 76 | Middlesbrough | Boro | Middlesbrough | Riverside Stadium | 34742 | 0.000
35 | 36 | 85 | Sheffield Wednesday | Owls | Sheffield | Hillsborough Stadium | 39732 | 0.000
37 | 38 | 87 | Stoke City | Potters | Stoke | Britannia Stadium | 27902 | 27.534
(4 rows)
premdb=> select * from team where city ~ 'po*';
teamid | htid | atid | name | nickname | city | stadium | capacity | avg_att
--------+------+------+-------------------------+-----------+---------------+--------------------+----------+---------
6 | 7 | 56 | Blackpool | Seasiders | Blackpool | Bloomfield Road | 17338 | 0.000
17 | 18 | 67 | Everton | Toffees | Liverpool | Goodison Park | 40221 | 38.124
20 | 21 | 70 | Ipswich Town | Blues | Ipswich | Portman Road | 30311 | 0.000
23 | 24 | 73 | Liverpool | Reds | Liverpool | Anfield | 44742 | 43.910
36 | 37 | 86 | Southampton | Saints | Southampton | St. Mary's Stadium | 32505 | 30.751
47 | 48 | 97 | Wolverhampton Wanderers | Wolves | Wolverhampton | Molineux Stadium | 31700 | 0.000
(6 rows)
premdb=> select * from team where city ~ 'po+';
teamid | htid | atid | name | nickname | city | stadium | capacity | avg_att
--------+------+------+-----------+-----------+-----------+-----------------+----------+---------
6 | 7 | 56 | Blackpool | Seasiders | Blackpool | Bloomfield Road | 17338 | 0.000
17 | 18 | 67 | Everton | Toffees | Liverpool | Goodison Park | 40221 | 38.124
23 | 24 | 73 | Liverpool | Reds | Liverpool | Anfield | 44742 | 43.910
(3 rows)
premdb=> select * from team where city ~ 'po{2,}';
teamid | htid | atid | name | nickname | city | stadium | capacity | avg_att
--------+------+------+-----------+-----------+-----------+-----------------+----------+---------
6 | 7 | 56 | Blackpool | Seasiders | Blackpool | Bloomfield Road | 17338 | 0.000
17 | 18 | 67 | Everton | Toffees | Liverpool | Goodison Park | 40221 | 38.124
23 | 24 | 73 | Liverpool | Reds | Liverpool | Anfield | 44742 | 43.910
(3 rows)
Parent topic:Regular Expression Details