I need to write a regular expression for an 'option symbol' for my company so we can validate these symbols across our site.
An option symbol is composed of two parts:
Part1 Part2
_ _ _ _ _ _ | _ _ _ _ _ _ _ _ _
I can write a regular expression for Part2 as it is fairly simple.
However, Part1 (the first 6 character positions) can be a little complicated.
It boils down to:
- Part1 must be {6} characters in length total.
- There must be between {1,4} alpha characters in the first positions.
- After that, there can optionally be {1} numeric character.
- Finally, the rest of the characters remaining must be spaces, so Part1 totals to 6 characters.
The problem I'm having is that the number of spaces is variable based on the number of characters before it. This makes me think it isn't easily representable by a regular language.
How can I avoid brute-forcing it like so:
([A-Za-z]{1}[0-9]{1}[ ]{4}|
[A-Za-z]{2}[0-9]{1}[ ]{3}|
[A-Za-z]{3}[0-9]{1}[ ]{2}|
[A-Za-z]{4}[0-9]{1}[ ]{1}|
[A-Za-z]{1}[ ]{5}|
[A-Za-z]{2}[ ]{4}|
[A-Za-z]{3}[ ]{3}|
[A-Za-z]{4}[ ]{2}|
[A-Za-z]{5}[ ]{1})
Here are some example option symbols (remember, ignore everything beyond the first 6 characters):
F 123456P12345678
CMG 123456P12345678
AAPL 123456P12345678
GOOG1 123456C12345678
F5 123456C12345678