How to do it instead?
(?: [a-zA-Z0-9] {6})? (317866?)
Or, if you know that a longer version always starts with a letter, you can use this:
(?: [a-zA-Z] [a-zA-Z0-9] {5})? (317866?)
First, it will try to match 6 alphanumeric characters, followed by 31786 or 317866, and if that fails, it will back off and try to match 31786 or 317866.
source share