Reputation: 640
I am new to RegEx and need to come up with a RegEx that will find matching character(s) in a String.
The possible strings that I could get are:
DFG-2344KG
4GGRTE/345
9TTRRE-547
7TTRRE-547
T89FGFGD+9
So what I want is a RegEx that will check to see if the string starts with DFG or 4 or 7 or T AND it does only have "-" for special characters other than alphanumeric characters.
So from the above list of strings, only 9TTRRE-547 will be a valid string.
I am looking for a RegEx that can do that for me. So far I came up with:
^(DFG|T|4|7)
The above RegEx correctly finds the invalid starting character(s). Now the challenge is finding a special character other than "-" that can happen anywhere in the string.
Any suggestions?
Upvotes: 1
Views: 62
Reputation: 290455
I would try with:
^(DFG|4|7|T)[A-Z0-9-]+$
It is exactly what you have so far together with [A-Z0-9-]+
to indicate: "any alpha numeric character or -
. By saying +
we mean that it occur one or many times.
Then, $
to indicate end of line.
You can see it working in https://regex101.com/r/mX5kV8/1 with these matches:
DFG-2344KG
7TTRRE-547
7TTRRE547
Upvotes: 1