Reputation: 1177
I have created the following search patterns:
1) Search numbers within given range and excludes specific numbers (excludes 1,2,8)
string numberPattern = @"^([3-7|9 ]*)$";
2) Search letters within given range and excludes specific characters (excludes B,V)
string characterPattern = @"^(?:(?![BV])[A-Z ])+$";
And there can be three kind of inputs:
ANRPIGHSAGASGG
34567934567967
9ANRPIG34HS56A
Question:
Is there a way to tell regex, if using number pattern then it ignores characters and same for character pattern, that it would ignore numbers? The data just can be mixed, in mixed order, I just don't see other way than grouping numbers and characters in different lists and then use related pattern.
Is there a way to accomplish that using only regex?
Upvotes: 1
Views: 178
Reputation:
Put it into a more readable state so you can maintain it.
^(?:[0-9A-Z](?<![128BV]))+$
Explained
^ # Beginning of string
(?: # Cluster group
[0-9A-Z] # Initially allow 0-9 or A-Z
(?<! [128BV] ) # Qualify, not 1,2,8,B,V
)+ # End cluster, must be at least 1 character
$ # End of string
Upvotes: 2
Reputation: 627607
I suggest using
^[3-79A-Z -[BV]]*$
See the regex demo.
Details:
^
- a start of a string anchor[3-79A-Z -[BV]]*
- zero or more (*
) characters:
3-79A-Z
- digits from 3
to 7
, 9
, uppercase ASCII letters and a space except B
and V
ASCII letters (the -[BV]
is a character class subtraction construct)$
- end of string anchor.Upvotes: 2