Simple C# regex question -
question: what's simplest way how test if given regex matches whole string ?
an example: e.g. given regex re = new regex("."); want test if given input string has 1 character using regex re. how do ?
in other words: i'm looking method of class regex works similar method matches() in class matcher in java ("attempts match entire region against pattern.").
edit: question not getting length of string. question how match whole strings regular exprestions. example used here demonstration purposes (normally check length property recognise 1 character strings).
if allowed change regular expression should surround ^( ... )$. can @ runtime follows:
string newre = new regex("^(" + re.tostring() + ")$"); the parentheses here necessary prevent creating regular expression ^a|ab$ not want. regular expression matches string starting a or string ending in ab.
if don't want change regular expression can check match.value.length == input.length. method used in asp.net regular expression validators. see answer here fuller explanation.
note method can cause curious issues should aware of. regular expression "a|ab" match string 'ab' value of match "a". though regular expression have matched whole string, did not. there warning in documentation.
Comments
Post a Comment