I’ve been working for many hours trying to do a “simple thing”: use a regex to validate a text field.
I need to make sure of:
1- Only use (a-z), (A-Z) and (0-9) values
2- Add a SINGLE wildcard only at the end.
Ex.
Match
MICHE*
Match
JAMES
No match
MICHE**
No match
MIC_HEAL*
I have this regex till now:
[a-zA-Z0-9\s-]+.\z*?
The problem is it still matches when I introduce an invalid character as long as I have a matching sub-string See my REGEX
What can I do to force a match on the whole string? What am I missing?
Thx!
Use
^(start of line) and$(end of line) to only match the whole string:(If you have a multiline input you can also use
\Aand\z– start and end of string)On a second look, I don’t understand the end of your regex:
.(anything)\z*?(end of string, zero or more times, zero or one time). This regex will match something like:Is that correct? If you only want the character
*, you should use:Also, as Robbie pointed out, you’re including spaces and the
-in your list of accepted characters. If you only want letters and digits, a shortcut would be using\w(word characters):However, depending on whether the matcher is Unicode-aware or not,
\wwill also match non-ASCII letters and digits, which may or may not be what you want.