user2175783
user2175783

Reputation: 1441

regex for not matching alpha plus numeric range

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).

Upvotes: 1

Views: 89

Answers (2)

Rakshith B S
Rakshith B S

Reputation: 45

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}

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

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

Upvotes: 4

Related Questions