0

The following RegEx [0-9]*[ -/]{0,1}[0-9]* matches e.g. 1*2.

The * is not defined in my regex and not intended; the space before - needs not to be escaped (as far as I found out)

If I only use two character of [ -/], 1*2 do not match.

When I change the order e.g. to [0-9]*[-/ ]{0,1}[0-9]*, 1*2 is not matching (like expected).

Do I miss something or is this a bug?

I have this behaviour with Java 7 and on http://www.regexplanet.com/advanced/java/index.html

Update The RegEx is used in the Bean Validation @Pattern(regexp = "[0-9][ -/]{0,1}[0-9]").

macruch
  • 41
  • 4

3 Answers3

4

[ -/] is a character class, and in a character class, - is the range operator. "any character in the range of characters from 'space' to 'slash', inclusive".

That means it uses the ASCII table (basically) to match the characters [space], !, ", #, $ etc... up to /.

However, ranges only work in a postive direction: from a low ASCII code to a high ASCII code. When you go high->low, the range doesn't apply, and it's looking for only 3 characters: [space], dash and slash.

E.g. in an easier to read example:

ascending:    [b-g] -> matches 'b', 'c, 'd', 'e', 'f', or 'g'
descending:   [g-b] -> matches ONLY 'g', '-', or 'b'
Marc B
  • 356,200
  • 43
  • 426
  • 500
3

I think the reason is due to the hyphen.

The section [ -/] is actually creating a range from space to /.

Sarima
  • 749
  • 7
  • 21
2

you have [ -/] means any char, between space and slash. (ascii 32 - 47). * has ascii 42, that's why 1*2 was matched.

If you want to match only - or / or SPACE , use [-/ ], put the - at the beginning in the character class.

Kent
  • 189,393
  • 32
  • 233
  • 301