Sunday, 15 May 2011

regex - What is the point of having * in a regular expression -


Recently I am thinking that we need * why * in regular expressions. For example, if we want to represent A0, A1 .., Z99, we can do this:

  [AZ] [0-9] [0-9] *  

but AA (which we do not want) is also valid as per the above. What do I gain?

* is just a quantifier, matching between zero and unlimited time.

[AZ] [0-9] [0-9] * corresponds to A0, A1 .., Z99 and also A10000, Z123456789 ...

Remember that if you do not enter ^ and $ as anchor, the processor will match the specified part, and True even if the input contains more characters, because you have not said that you want only positive results, if the whole input matches regex.

If your goal only matches A0, A1., Z99 should be regex:

  ^ [AG] [0-9] [0-9] ? $  

Or simply:

  ^ [az] \ d {1,2} $  

\ D means 'digit', and that is equal to [0-9] as {1,2} means at least 1 time Happens and nothing more than 2 times. ? is also a quantifier, which matches 0 or 1 time.


No comments:

Post a Comment