Reputation: 67
Recently I ran into a validation situation I've been trying to solve with regex. The rules are as such:
I have attempted to match this string with the following regex:
^(?!_{2,})([A-Z][a-zA-Z0-9_]*[0-9])$
and
^(?<=_{0,1})([A-Z][a-zA-Z0-9_]*[0-9])$
Both of these attempts still match cases where there is more than one underscore present. I.E. App_l_e9
or App__le9
.
How can you check to see if your regex match, I.E. the ([A-Z][a-zA-Z0-9_]*[0-9])
part contains zero or one underscore in any place within the middle of the string?
Upvotes: 2
Views: 823
Reputation: 163362
You might also start the match with an uppercase A-Z and immediately check that the string ends with a number 0-9 using a positive lookahead to prevent catastrophic backtracking.
^[A-Z](?=.*[0-9]$)[a-zA-Z0-9]*_?[a-zA-Z0-9]*$
^
Start of string[A-Z]
Match an uppercase char A-Z(?=.*[0-9]$)
Positive lookahead to assert a digit 0-9 at the end of the string[a-zA-Z0-9]*
Optionally match any of the listed_?
Match an optional _
[a-zA-Z0-9]*
Optionally match any of the listed$
End of stringOr with an optional group
^[A-Z](?=.*[0-9]$)[a-zA-Z0-9]*(?:_[a-zA-Z0-9]*)?$
Upvotes: 1
Reputation: 17390
The simplest approach would probably be this
^[A-Z][a-zA-Z0-9]*_?[a-zA-Z0-9]*[0-9]$
Explanation:
^[A-Z]
Must start with an uppercase letter[a-zA-Z0-9]*
A combination of uppercase and lowercase letters and numbers of any length (also 0-length)_?
Either zero or one underscore character[a-zA-Z0-9]*
Again A combination of uppercase and lowercase letters and numbers of any length (also 0-length)[0-9]$
Must end with a numberThis will accept A_9
or AA0_xY8
but for instance not aXY_34
or Aasf1__asdf5
If the underscore in the middle part must not be the first or last character of this middlepart, you can replace the *
with a +
like this.
^[A-Z][a-zA-Z0-9]+_?[a-zA-Z0-9]+[0-9]$
So this, won't accecept for instance A_9
anymore, but the word must at least be Ax_d9
Upvotes: 2