Can anybody help me with a regex? I have a string with digits like:
X024123099XYAAXX99RR
I need a regex to check if a user has inserted the correct information. The rule should have also a fallback that the input is checked from left to right.
For example, when tested these inputs should return TRUE:
X024
X024123099X
X024123099XYA
X024123099XYAAXX99R
And these ones should return FALSE:
XX024
X02412AA99X
X024123099XYAAXX9911
And so on. The regex must check for the correct syntax, beginning from the left.
I have something like that, but this seems not to be correct:
\w\d{0,12}\w{0,6}\d{0,2}\w{0,2}
Big thanks for any help (I’m new to regex)
You could take OpenSauce’s regex and then hack it to pieces to allow partial matches:
It’s not pretty but as far as I can tell it encodes your requirements.
Essentially I took each case of something like
\d{9}and replaced it with something like(\d{0,9}$|\d{9}<rest of regex>).I added
^and$because otherwise it will match substrings in an otherwise invalid string. For example, it will see an invalid string likeXX024and think it is okay because it containsX024.