Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between '?' and '*' in regular expressions

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:

  • if we can match 0 or more by '*' then why should we use '?' meta character?
  • We define Float as : FL [0-9]*"."[0-9]+
  • Can we define it as : 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 :).

    like image 224
    Vedant Terkar Avatar asked Dec 01 '13 07:12

    Vedant Terkar


    People also ask

    What does '?' Mean in regular expression?

    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).

    What does '?' quantifier represent in regex?

    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*?

    What is difference [] and () in regex?

    [] 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 .

    What is the difference between A *+ B * and a B )* in regular expression?

    ( 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.


    1 Answers

    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.

    like image 104
    T.J. Crowder Avatar answered Sep 30 '22 15:09

    T.J. Crowder