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)
4
  • 2
    try [-LBQTHWROMSNT](0MIL|[LBCDGJKMNPQSTUVWXYZ][NA][0137CUVRT][PCBMVWTHKNDG])(NR|RR)(?:(?<=A[A-Z0-9]{4})[OPLH]|$) Commented Aug 21, 2014 at 15:05
  • 3
    Java's regexes are documented in the Pattern 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. Commented Aug 21, 2014 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:regex101.com/r/qC1jJ3/1 Commented Aug 21, 2014 at 15:28
  • 1
    The canonical Q: stackoverflow.com/q/8072756/1296806 Commented Aug 21, 2014 at 20:16

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

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.