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?
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?
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.
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>
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.