I am learning regular expressions to use them in lex program. I've Seen here that, in Regular Expressions:
'*' matches 0 or more occurances of pattern
'?' matches 0 or 1 occurance of the pattern
By this I'm kinda Confused. I mean:
FL [0-9]*"."[0-9]+
FL [0-9]?"."[0-9]+
for numbers like 0.999 or .999 etc (ie, Number with only one digit before radix point .
)? Can Any one please explain this? Thanking you in advance :).
Show activity on this post. ^ means "Match the start of the string" (more exactly, the position before the first character in the string, so it does not match an actual character). $ means "Match the end of the string" (the position after the last character in the string).
The *? quantifier matches the preceding element zero or more times but as few times as possible. It's the lazy counterpart of the greedy quantifier * . In the following example, the regular expression \b\w*?
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
( a + b )* corresponds to the set of all strings over the alphabet {a, b}. a*b* corresponds to the set of strings consisting of zero or more a's followed by zero or more b's. a*b+a* corresponds to the set of strings consisting of zero or more a's followed by one or more b's followed by zero or more a's.
If you want to match 0, 1, 2, 3, 4, 5, 6, or more occurrences, use *
.
If you only want to match 0 or 1 occurrences, use ?
.
For instance, consider this text: "________starts with whitespace"
If I want to match all of the underscores at the beginning of that text, but I don't want to require that they be there (they're optional), I'd use _*
.
In contrast, if I to just match an optional single +
in (say) "+44 20 1234 5678"
, I'd use \+?
(a literal +
with the ?
after it). That will only match the single +
or nothing, it would not match multiple +
characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With