How do I use regex properly? Why isn't this working? [duplicate]
Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
string regex = "<Name[.\\s]*>[.]*s[.]*</Name>";
string source = "<Name xmlns=\"http://xml.web.asdf.com\">Session</Name>";
bool hit = System.Text.R开发者_如何学编程egularExpressions.Regex.IsMatch(
source,
regex,
System.Text.RegularExpressions.RegexOptions.IgnoreCase
);
Why is hit
false? I'm trying to find any Name
XML field that has an 's'
in the name. I don't understand what could be wrong.
Thanks!
You are using .
in a character class, where it means literally .
, I think you mean to use in the sense of any character - so .*
rather than [.]*
string regex = "<Name(.|\\s)*>.*s.*</Name>";
With XPath, this could be as easy as /Name[contains(.,'s')]
精彩评论