Vedant Terkar
Vedant Terkar

Reputation: 4682

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

    Upvotes: 5

    Views: 14260

    Answers (1)

    T.J. Crowder
    T.J. Crowder

    Reputation: 1075597

    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.

    Upvotes: 11

    Related Questions