Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding boundary with a preceding word in regex

Tags:

regex

I have a regex that detects something called a PO Number that is PO Number followed by some characters that may or may not be digits but certainly contains a digit. It can also have spaces.

You can see the link to find the problem.

Pattern:

((P\s*o\s*(\#|Number|No|\.)*\s*([:=\-\'\s\.])?\s*(\d*\s?([A-Z\-\/]*\s?\d+)+[A-Z\-]*)))

The problem arises when there is something like:

Case 3. Ship on 123

Here p is followed by o and the following conditions are good for match. But I needed to check the condition preceding 'p' that is 'shi'p.

Clearly, I needed to implement something to avoid p preceded by a plain word [A-Z]+ but allow p if its preceded by a space or a digit. Following conditions should pass.

Case 1. YO#11111PO#2015 S53-55A

Case 2. YO#12345_PO#234 S34-34A

I tried using word boundary \b but that causes case 1 and 2 to fail. Is there another way.

like image 745
Identity1 Avatar asked Dec 05 '25 03:12

Identity1


1 Answers

Try preceding your regexp with (?:[^a-z])

This is a non-capturing group so when you interpret your groups it won't appear in the list.

like image 107
Tom Winch Avatar answered Dec 07 '25 18:12

Tom Winch



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!