开发者

How do I use regex properly? Why isn't this working? [duplicate]

开发者 https://www.devze.com 2023-04-07 13:07 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: RegEx match open tags except XHTML self-contained tags
This question already has answers here: Closed 11 years ago.

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')]

0

精彩评论

暂无评论...
验证码 换一张
取 消