I am trying to write a regular expression to match a form of string :
"[A-Za-z][A-Za-z]-[A-Za-z][A-Za-z]_[match all chars]"
The string I want to match must be of this form, including the hyphen and underscore. So far I have :
Regex regEx = new Regex(@"[A-Za-z]+(-[A-Za-z]+)+*$", RegexOptions.IgnorePatternWhitespace);
I am not sure how to add in the underscore character so that this is matched.
This element is an XML element I wish to match; I would also like to retrieve the contents of this element. How could I do this ?
var newVar = from e in doc.Descendants("DocumentElement").Descendants()
where regEx.IsMatch(e.Name.LocalName)
select e;
I’d use
Explanation:
^beginning of the line[A-Za-z]{2}exactly 2 characters must be a-zA-Z-the dash_the underscore.*any character$end of the lineEDIT:
See also Regex docs