1

I have the following regex

.{19}_.{3}PDR_.{8}(ABCD|CTNE|PFRE)006[0-9][0-9].{3}_.{6}\.POC

a match is for example

NRM_0157F0680884976_598PDR_T0060000ABCD00619_00_6I1N0T.POC

and would like to negate the (ABCD|CTNE|PFRE)006[0-9][0-9] portion such that

NRM_0157F0680884976_598PDR_T0060000ABCD00719_00_6I1N0T.POC

is a match but

NRM_0157F0680884976_598PDR_T0060000ABCD007192_00_6I1N0T.POC

or

NRM_0157F0680884976_598PDR_T0060000ABCD0061_00_6I1N0T.POC

is not (the negated part must be 9 chars long just like the non negated part for a total length of 58 chars).

1

2 Answers 2

4

Consider using the following pattern:

\b(?:ABCD|CTNE|PFRE)006[0-9][0-9]\b

Sample Java code:

String input = "Matching value is ABCD00601 but EFG123 is non matching";
Pattern r = Pattern.compile("\\b(?:ABCD|CTNE|PFRE)006[0-9][0-9]\\b");
Matcher m = r.matcher(input);
while (m.find()) {
    System.out.println("Found a match: " + m.group());
}

This prints:

Found a match: ABCD00601
Sign up to request clarification or add additional context in comments.

2 Comments

This doesnt seem to work. Here it is my full regex .{19}_.{3}PDR_.{8}\b(?:ABCD|CTNE|PFRE)006[0-9][0-9]\b.{3}_.{6}\.POC. The following string fails but should be a match NRM_0157F0680884976_598PDR_T0060000ABCD00719_00_6I1N0T.POC
Here it is my full regex ... where does my answer ever suggest this regex?
3

I would like to propose this expression (ABCD|CTNE|PFRE)006\d{1,2}

where \d{1,2} catches any one or two digit number that is it would get any alphanumeric values from ABCD0060~ABCD00699 or CTNE0060~CTNE00699 or PFRE0060~PFRE00699

Edit #1:

as user @Hao Wu mentioned the above regex would also accept if its ABCD0060 which is not ideal so this should do the job by removing 1 from the { } we can get

alphanumeric values from ABCD00600~ABCD00699 or CTNE00600~CTNE00699 or PFRE00600~PFRE00699 so the resulting regex would be

(ABCD|CTNE|PFRE)006\d{2}

2 Comments

But it makes a false positive ABCD0060?
In the previous regex \d{1,2} would get any digit from 0-99 so I made changes to it such that \d{2} would now get any digit from 00-99,hope this fixes the issue for ABCD0060

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.