Mikroelektronika MIKROE-350 データシート
Metacharacters - Iterators
Any item of a regular expression may be followed by another type of metacharac-
ters - iterators. Using this metacharacters,you can specify number of occurences of
previous character, metacharacter or subexpression.
ters - iterators. Using this metacharacters,you can specify number of occurences of
previous character, metacharacter or subexpression.
*
- zero or more ("greedy"), similar to {0,}
+
- one or more ("greedy"), similar to {1,}
?
- zero or one ("greedy"), similar to {0,1}
{n}
- exactly n times ("greedy")
{n,}
- at least n times ("greedy")
{n,m}
- at least n but not more than m times ("greedy")
*?
- zero or more ("non-greedy"), similar to {0,}?
+?
- one or more ("non-greedy"), similar to {1,}?
??
- zero or one ("non-greedy"), similar to {0,1}?
{n}?
- exactly n times ("non-greedy")
{n,}?
- at least n times ("non-greedy")
{n,m}?
- at least n but not more than m times ("non-greedy")
So, digits in curly brackets of the form,
{n,m}
, specify the minimum number of times to
match the item n and the maximum
m.
The form
{n}
is equivalent to
{n,n}
and match-
es exactly
n
times. The form
{n,}
matches
n
or more times. There is no limit to the size
of
n
or
m
, but large numbers will chew up more memory and slow down execution.
If a curly bracket occurs in any other context, it is treated as a regular character.
Examples:
count.*r ß
- matches strings like
'counter', 'countelkjdflkj9r'
and
'countr'
count.+r
- matches strings like
'counter', 'countelkjdflkj9r'
but not
'countr'
count.?r
- matches strings like
'counter', 'countar'
and
'countr'
but not
'countelkj9r'
counte{2}r
- matches string
'counteer'
counte{2,}r
- matches strings like
'counteer', 'counteeer', 'counteeer'
etc.
counte{2,3}r
- matches strings like
'counteer'
, or
'counteeer'
but not
'coun-
teeeer'
A little explanation about "greediness". "Greedy" takes as many as possible, "non-
greedy" takes as few as possible.
For example,
greedy" takes as few as possible.
For example,
'b+'
and
'b*'
applied to string
'abbbbc'
return
'bbbb', 'b+?
'
returns
'b', 'b*?'
returns empty string,
'b{2,3}?'
returns
'bb', 'b{2,3}'
returns
'bbb'
.
65
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Environment
mikroBasic PRO for AVR
CHAPTER 2