0

I have a regular expression for Canadian zip code. But this regual expression support only upper case letters not lower case letters. Can any one please let me know how this support lower case characters.

reg exp:

Pattern patternForZip = Pattern.compile("^(?!.*[DFIOQU])[A-VXY][0-9][A-Z]?[0-9][A-Z][0-9]$");

How this above reg exp supports lower case letters.

RKCY
  • 4,095
  • 14
  • 61
  • 97

1 Answers1

0

Try this:

"^(?!.*[DFIOQUdfioqu])[A-VXYa-vxy][0-9][A-Za-z]?[0-9][A-Za-z][0-9]$"

Or can do this to make it case insensitive:

"(?!)^(?!.*[DFIOQU])[A-VXY][0-9][A-Z]?[0-9][A-Z][0-9]$"

You can try this:

final String regex = "^(?!.*[DFIOQU])[A-VXY][0-9][A-Z]?[0-9][A-Z][0-9]$";
final String string = "a2a2a2\n"
        + "A2A2A2";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
}
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43