why below regular expression doesn't match the string?

Viewed 33

I am trying to match the below string with Regular expression

string : PKGx.1234 ... BBA Regular expression : ^\bPKG[0-9]{0,1}.[0-9]{0,4}\ ...\ \bBB[A-B]{1}?$

but i am getting no match error

can anyone help me with how can i remodify the regular expression to match the given string ..?

1 Answers

You have a character x after PKG that the pattern tries to match with an optional digit [0-9]? If there should be an optional single a lowercase character, you can use [a-z]? instead.

You can omit the word boundary before the BB as there is an implicit word boundary in between.

Note that you don't have escape the spaces, but you do have to escape the dot to match it literally.

^PKG[a-z]?\.[0-9]{0,4} \.{3} BB[A-B]\b

Regex demo

If you want to match the whole string including the space and comma at the end, including using the $ anchor to assert the end of the string:

^PKG[a-z]?\.[0-9]{0,4} \.{3} BB[A-B] , *$

Regex demo

Related