using System.Text.RegularExpressions;
...
private bool IsNumeric(string s)
{
Regex r = new Regex(@"^[0-9]+$");
return r.IsMatch(s);
}
The regular expression defines a pattern and the IsMatch method checks the string to see if it matches the pattern. You can read the regular expression as follows:
^ – start of the string [0-9] – single digit in the range 0-9 + – repeat previous item one or more times (single digit) $ – end of the string
If you are using below Regex for the same purpose
Regex regex = new Regex(@"^\d+$");
please remember that "\d" will match [0-9] and other digit characters like the Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩. Use "^[0-9]+$" to restrict matches to just the Arabic numerals 0 - 9. It is explained here.
No comments:
Post a Comment