0

I want to extract all the texts between the specified opening and closing tags including the tags. For eg:

Input : I am <NAME>Kai</NAME>
Text Extracted: <NAME>Kai</NAME>

It extract the text based on tag.

What is Regex for the above?

Kai
  • 2,967
  • 1
  • 22
  • 28
  • 2
    possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Filip Ekberg Feb 10 '11 at 08:12

3 Answers3

3

If the tag in question can't be nested (and assuming case insensitivity):

Regex regexObj = new Regex("<NAME>(?:(?!</NAME>).)*</NAME>", RegexOptions.Singleline | RegexOptions.IgnoreCase);

Be advised that this is a quick-and-dirty solution which might work fine for your needs, but might also blow up in your face (for example if tags occur within comments, if there is whitespace inside the tags, if there are any attributes inside the tags etc.). If any of these might be a problem for you, please edit your question with the exact specifications you need the regex to comply with.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

Here is a regex which accepts any tag name: <(\w+)>.*?</\1>

\1 is back-referencing the group (\w+) and ensures that the closing tag must have the same name as the opening tag.

If you want to search for the special tag NAME then you could use this regex: <NAME>.*?</NAME>

splash
  • 13,037
  • 1
  • 44
  • 67
0

http://www.regular-expressions.info/reference.html You might find something useful here, they have allot of stuff specially for tags etc. Combine the examples to meet your requirements.

Ahsan
  • 459
  • 8
  • 22