c# - Regular expression to match a string that contains only numbers, not letters -
my code using following regex expression matches on numbers:
regex numberexpression = new regex(@"(?<number>\d+)");
this current works fine input strings "1", "100", "1a", "a1", etc....
but want change not match when input string contains letter, "1", "100" match, "1a", "a1", not.
can help, know simple regular expression question can't head around forward , backward looking. have tried:
regex numberexpression = new regex(@"(?<number>^![a-za-z]\d+![a-za-z])");
but didn't work, , fails match of above input.
you trying hard way, looking numeric substring of input, , looking see there isn't before or after substring.
the easy way force regular expression either match entire input string or nothing:
regex numberexpression = new regex(@"^\d+$");
where "^" means "beginning of line" , "$" means "end of line".
Comments
Post a Comment