c# - Regular expression for specific number of digits -
i want write regular expression in c# inputs specific number of numbers.
like writing regular expression validate 5 digits number "12345"
use following regex regex.ismatch
method
^[0-9]{5}$
^
, $
anchors match beginning , end of string (respectively) prevent match found in middle of long string, such 1234567890
or abcd12345efgh
.
[0-9]
indicates character class specifies range of characters 0
9
. range defined unicode code range starts , ends specified characters. {5}
followed behind quantifier indicating repeat [0-9]
5 times.
note solution of ^\d{5}$
equivalent above solution, when regexoptions.ecmascript
specified, otherwise, it equivalent \p{nd}
, matches unicode digits - here the list of characters in nd
category. should check documentation of language using shorthand character classes matches.
i suggest read through documentation. can use other resources, such http://www.regular-expressions.info/, always check on documentation of language using.
Comments
Post a Comment