0

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.RegularExpressions.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!

Community
  • 1
  • 1
Tizz
  • 820
  • 1
  • 15
  • 31
  • 7
    Dude, are you trying to parse XML/XHTML with Regex? Were you aware of this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 Please do something before the `
    ` crashes.
    – Darin Dimitrov Sep 26 '11 at 20:43
  • 3
    you should probably read this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – scottm Sep 26 '11 at 20:43
  • See cthulu: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – CassOnMars Sep 26 '11 at 20:43
  • Im not trying to parse HTML, the 'source' is a class Serialized. I think I can handle regex with a serialized class, no? – Tizz Sep 26 '11 at 20:48
  • 1
    @Tizz - When dealing with XML you use an XML parser and the provided API for querying the document. Regex adds needless complexity to a simple problem. – ChaosPandion Sep 26 '11 at 20:51
  • If it's serialized, why not just deserialize it? – Austin Salonen Sep 26 '11 at 20:52
  • 4
    Parsing XML with RegEx is like truck racing, it works but it's not meant to be used that way. – Bojan Kogoj Sep 26 '11 at 20:56

2 Answers2

4

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>";
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
2

With XPath, this could be as easy as /Name[contains(.,'s')]

scottm
  • 27,829
  • 22
  • 107
  • 159