Use regex to find specific string not in html tag

后端 未结 8 1095
温柔的废话
温柔的废话 2021-02-05 08:53

I\'m having some difficulty with a specific Regex I\'m trying to use. I\'m searching for every occurrence of a string (for my purposes, I\'ll say it\'s \"mystring\") i

相关标签:
8条回答
  • 2021-02-05 09:19

    Regular expression searches are typically not a good idea in XML. It's too easy to run into problems with search expressions matching to much or too little. It's also almost impossible to formulate a regex that can correctly identify and handle CDATA sections, processing instructions (PIs), and escape sequences that XML allows.

    Unless you have complete control over the XML content you're getting and can guarantee it won't include such constructs (and won't change) I would advise to use an XML parser of some kind (XDocument or XmlDocument in .net, for instance).

    Having said that, if you're still intent on using regex as your search mechanism, something like the following should work using the RegEx class in .NET. You may want to test it out with some of your own test cases at a site like Regexlib. You may also be able to search their regular expression catalog to find something that might fit your needs.

    [>].(_mystring_).[<]

    0 讨论(0)
  • 2021-02-05 09:23
    _mystring_(?![^<]*?>)
    

    But a valid HTML structure is required.

    0 讨论(0)
提交回复
热议问题