Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the two regex pattern

Tags:

java

regex

I was trying to validate a company name in a web application and had this regex pattern

^[a-zA-Z_'\\s,;.-0-9]{1,100}$

The above pattern will reject the value 10004 Estates Limited

But if I bring forward 0-9 so the pattern becomes

^[a-zA-Z0-9_'\\s,;.-]{1,100}$

then it works. Am new to regex and patterns, but I know I should be using more of it, so I want to be clear on this. Thanks.

like image 215
nwolisa Avatar asked Dec 02 '25 05:12

nwolisa


2 Answers

- is a special character in character classes and thus .-0-9 is ambiguous and probably gets the meaning . to 0 and - and 9, so essentially the characters ./09-.

To include a hyphen-minus in a character class you'd either have to escape it or place it at the start or end of the character class (which is what you're doing in the second regex, maybe by accident).

Edited to add: Above guess seems to be correct, at least for .NET's regex engine:

PS> [char[]](32..127) -match '[a-zA-Z_''\s,;.-0-9]'

'
,
-
.
/
0
9
;
A
...
like image 131
Joey Avatar answered Dec 03 '25 21:12

Joey


It's probably because of the "-" inside the character group in the first one

^[a-zA-Z_'\s,;.\-0-9]{1,100}$

Escaspe it and it should be fine.

Remember, when inside the character group the chars you have to escape become

backslash \
caret ^
hyphen -
like image 45
AlanFoster Avatar answered Dec 03 '25 22:12

AlanFoster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!