Conditional Regular Expression in Java?

前端 未结 2 1876
-上瘾入骨i
-上瘾入骨i 2020-12-19 07:44

I have a conditional regular expression that works on regex test websites, such as regexlib.com, but cannot get it to work in my Java application.

But, http://www.

相关标签:
2条回答
  • 2020-12-19 08:23

    How about just doing this instead?

    (?:[a-zA-Z0-9]{6})?(317866?)
    

    Or if you know that the longer version always start with a letter then you can use this:

    (?:[a-zA-Z][a-zA-Z0-9]{5})?(317866?)
    

    It will first try to match 6 alphanumerics followed by 31786 or 317866, and if that fails it will then backtrack and try matching 31786 or 317866.

    0 讨论(0)
  • 2020-12-19 08:37

    Conditional expressions are not supported by java.util.regex.Pattern class. To get around that you could use a 3rd party regexp library such as JRegex

    0 讨论(0)
提交回复
热议问题