Home > database >  regex for not matching alpha plus numeric range
regex for not matching alpha plus numeric range

Time:10-06

I would like to detect strings that

  1. Have certain substrings of the form ABCD00601 where ABCD can be one of 3 possible strings lets say ABCD, CTNE, PFRE and 00601 is in a range between 00600-00699
  2. Do not have substrings as in 1.

For 1. I thought of

.*(ABCD006[0-9][0-9]|CTNE006[0-9][0-9]|PFRE006[0-9][0-9]).*

and for 2. of

.*(?!ABCD006[0-9][0-9])(?!CTNE006[0-9][0-9])(?!PFRE006[0-9][0-9]).*

it seems 1 and 2 dont work though.

CodePudding user response:

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

CodePudding user response:

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 ABCD00600~ABCD00699 or CTNE00600~CTNE00699 or PFRE00600~PFRE00699

could you elaborate your 2nd condition.

  • Related