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.
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.
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