2

I'm trying to run this regular expression statement in Java. It should return false because the regex mandates there should only be an O,P,L, or H at the eighth position if there is an "A" in the third position.

System.out.println("TLN7HRNO".matches("[-LBQTHWROMSNT](0MIL|[LBCDGJKMNPQSTUVWXYZ][NA][0137CUVRT][PCBMVWTHKNDG])(NR|RR)(?(?<=A[A-Z0-9]{4})[OPLH]|$)"));

However, Java is not liking the IF-THEN-ELSE statement even though my regex editor worked just fine with it.

Does Java have a different implementation for IF-THEN-ELSE or does it just not support it all all.

java.util.regex.PatternSyntaxException: Unknown inline modifier near index 81
...[PCBMVWTHKNDG])(NR|RR)(?(?<=A[A-Z0-9]{4})[OPLH]|$)
                           ^                                                 
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.matches(Unknown Source)
at java.lang.String.matches(Unknown Source)
at com.swa.rm.pricing.PFCLInterface.launchCLInterface(PFCLInterface.java:45)
at com.swa.rm.pricing.PFCLInterface.main(PFCLInterface.java:24)
tmn
  • 9,565
  • 10
  • 49
  • 99
  • 2
    try `[-LBQTHWROMSNT](0MIL|[LBCDGJKMNPQSTUVWXYZ][NA][0137CUVRT][PCBMVWTHKNDG])(NR|RR)(?:(?<=A[A-Z0-9]{4})[OPLH]|$)` – Avinash Raj Aug 21 '14 at 15:05
  • 3
    Java's regexes are documented in the [`Pattern`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html) class. Any syntax that isn't listed there, isn't supported. Note that there is no (?X) (but as noted above, (?:X) might be what you're looking for. – yshavit Aug 21 '14 at 15:06
  • 2
    `(?X)` and `(?:X)` are two wholly different things. OP is looking for an IFTHENELSE regex statement not a non-capturing group. @yshavit look here:http://regex101.com/r/qC1jJ3/1 – skamazin Aug 21 '14 at 15:28
  • It works! Thanks guys. – tmn Aug 21 '14 at 15:34
  • 1
    The canonical Q: http://stackoverflow.com/q/8072756/1296806 – som-snytt Aug 21 '14 at 20:16

1 Answers1

1

There's a syntax error in your Pattern, as mentioned in the comments. Here's a walk-through:

[-LBQTHWROMSNT](0MIL|[LBCDGJKMNPQSTUVWXYZ][NA][0137CUVRT][PCBMVWTHKNDG])(NR|RR)(?(?<=A[A-Z0-9]{4})[OPLH]|$)

Firstly, there's abundance in your character classes. [-LBQTHWROMSNT] can be shortened to [-L-OBQ-THW]; [LBCDGJKMNPQSTUVWXYZ] use a range at the last character section to become [LBCDGJKMNPQS-Z]. (NR|RR) can be changed to ([NR]R) as | alternation follows a backtracking nature and [] lists are optimal and well-suited for usages like these.

The pattern syntax error is caused by (?(?<=. This is because in regex patterns, (? is a special pattern syntax. The next character following ? will determine the nature of this group, and it must form a special syntax group - otherwise ? becomes a quantifier and it fails to follow a quantifiable token.
In this case, your pattern failed to compile as (?( is no accepted syntax in Java.

You might have meant to use a non-capturing group instead. -> (?: )

Read more:

Community
  • 1
  • 1
Nordehinu
  • 338
  • 1
  • 3
  • 11