I have a regular expression
"[a-z][0-9][a-z][0-9][a-z][0-9]"
From this i want to generate a regular expression exclude the characters D
, F
, I
, O
, Q
and U
.
That is "Q3D4R5"
开发者_运维知识库should not be included.
How should i do this?
Just use a negative lookahead to exclude those characters from your string.
(?!.*[DFIOQU])(?:[a-z][0-9]){3}
http://www.rubular.com
Some explanations:
(?!.*[DFIOQU])
checks the complete string if there is a occurrence of a character from the character group, if so then the whole thing is False.
(?:)
defines a non capturing group that is repeated 3 times {3}
.
Your regex matches only lowercase characters, so I assume you want to exclude d, f, i, o, q and u .
You can rewrite your original regex as
([a-z][0-9]){3}
This regex you can modify to
([a-ceghj-nprstv-z][0-9]){3}
to achieve what you want
You can use
[a-ceghj-nprsv-z]
instead of
[a-z]
if case matters then to filter the letters part:
[a-zA-CEGHJ-NPRSV-Z]
this will allow any lower case letter but not the upper case ones specified.
as suggested in other answers, you can include specific characters as well as more specific ranges in suare brackets; assuming your regexp matching is not case-sensitive (otherwise your original example presumably wouldn't be working), you could have:
"[a-ceghj-nprsv-z][0-9][a-ceghj-nprsv-z][0-9][a-ceghj-nprsv-z][0-9]"
Now if you also want to validate length (if there's any possibility that you had a value like "A1A1A1A", and you did NOT want that to match), then you'd also need to detect the start and end of the expression, eg:
"^[a-ceghj-nprsv-z][0-9][a-ceghj-nprsv-z][0-9][a-ceghj-nprsv-z][0-9]$"
I checked the following two cases at http://tools.netshiftmedia.com/regexlibrary/ and they appear to work.
^(?!.D.)^(?!.F.)^(?!.I.)^(?!.O.)^(?!.Q.)^(?!.U.)[a-z][0-9][a-z][0-9][a-z][0-9]
[a-ce-ghj-npr-tv-z][0-9][a-ce-ghj-npr-tv-z][0-9][a-ce-ghj-npr-tv-z][0-9]
I have found this "regular expression editor" web site: http://www.rubular.com/
very useful.
精彩评论