tmajor99
asked on
Regular Expression to detect non-numeric chars and nulls
I need a regular expression that would check if a field contains any non-numeric values (i.e., alpha) and nulls.
UPC
---------
N/A ---> Should match REGEX
123456789012 ---->Would not match REGEX
----->Should Match Regex (null or space)
--- ---> Should match REGEX
UPC
---------
N/A ---> Should match REGEX
123456789012 ---->Would not match REGEX
----->Should Match Regex (null or space)
--- ---> Should match REGEX
I would do the inverse match all numeric and invert the response using the not operator
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@David
I agree if by "not" operator, you mean a negative look-ahead.
^(?!\d+$)
I agree if by "not" operator, you mean a negative look-ahead.
^(?!\d+$)
@JonNorman
I would not check the spaces. \D already takes care of that and will stop at the first whitespace it finds
And you don't need to check more than one non-numerical char
^$|\D
I would not check the spaces. \D already takes care of that and will stop at the first whitespace it finds
And you don't need to check more than one non-numerical char
^$|\D
OK, I get the point of not looking for optional whitespace, but I continue to look for 1 or more non digit characters, in the point of view that you would want to check the whole string to find all the problems and inform the user of all the problems unless you want to just reply with any problem and then stop at the first match for simplicity.
Jon
Jon
Checking for one non-digit character is enough to know that there is at least one non-digit character. If you only want to know if your text matches the regex, the result is exactly the same, except it's faster.
The only difference is if you want to get the matched text: \D returns the first non-digit character, \D+ returns the first sequence of non-digit characters.
The only difference is if you want to get the matched text: \D returns the first non-digit character, \D+ returns the first sequence of non-digit characters.
Agreed, but if you want all the matches....
Open in new window
Match an empty input, any word character, any whitespace character, or a null. This will not catch everything in the ASCII character set but it will catch the most likely ones. For a comprehensive regex,
Open in new window
Match an input that is empty, any control character including nulls, or non-numeric.
These regexes assume that the input is ASCII.