Albert Laure
Albert Laure

Reputation: 1722

Special characters cannot be identified by Reg Ex

I have this Reg Ex

\b\d{7}PM\w{2}\d*\b

so based on the reg ex it should only accept
7 Numbers + PM + 2 alphanumeric + Any Length of Number. so this will accepts such as this respectively as it has \b in the beginning and in the end

1032213PM39849723  

My question is that why does the regex above accepts

1032213PM39849723<\test>

or

1032213PM39849723.

or

>1032213PM39849723

and not

A1032213PM39849723

or

1032213PM39849723K

it really bugs me why is this happening, if there are any changes to be made in the regex so that any other characters would not be accepted at the end or the beginning please tell me so.

Upvotes: 1

Views: 38

Answers (1)

deceze
deceze

Reputation: 522382

It depends on the flavour of regex function you use, but typically that regex does not mean that the entire string has to consist of this and only this, it merely means that some part of the string has to match this. If you want to match the whole string, add start and end anchors:

^\d{7}PM\w{2}\d*$

Upvotes: 3

Related Questions