tags:

views:

476

answers:

4

Hello there,

I am trying to get all strings enclosed in <*> by using following Regex:

Regex regex = new Regex(@"\<(?<name>\S+)\>", RegexOptions.IgnoreCase);
string name = e.Match.Groups["name"].Value;

But in some cases where I have text like :

<Vendors><Vtitle/>  <VSurname/></Vendors>

It's returning two strings instead of four, i.e. above Regex outputs

<Vendors><Vtitle/> //as one string and 
<VSurname/></Vendors> //as second string

Where as I am expecting four strings:

<Vendors>
<Vtitle/>
<VSurname/>
</Vendors>

Could you please guide me what change I need to make to my Regex.

I tried adding '\b' to specify word boundry

new Regex(@"\b\<(?<name>\S+)\>\b", RegexOptions.IgnoreCase);

, but that didn't help.

+5  A: 

Regexes are the wrong tool for parsing XML. Try using the System.Xml.Linq (XElement) API.

Manu
See Dennis Palmer's comment on the original question. This isn't XML.
Cheeso
+7  A: 

You'll get most of what what you want by using the regex /<([^>]*)>/. (No need to escape the angle brackets' as angle brackets aren't special characters in most regex engines, including the .NET engine.) The regex I provided will also capture trailing whitespace and any attributes on the tag--parsing those things reliably is way, way beyond the scope of a reasonable regex.

However, be aware that if you're trying to parse XML/HTML with a regex, that way lies madness

JSBangs
+1 for link to the one and only answer to this question.
Greg D
By answering this question, however, the OP might use this regex (and more regexes) instead of the better methods. Then 2-3 years down the road someone's going to have to maintain it.
Fragsworth
+1  A: 

Your regex is using \S+ as the wildcard. In english, this is "a series of one or more characters, none of which is non-whitespace". In other words, when the regex <(?<name>\S+)> is applied to this string: '`, the regex will match the entire string. angle brackets are non-whitespace.

I think what you want is "a series of one or more characters, none of which is an angle bracket".

The regex for that is <(?<name>[^>]+)> .

Ahhh, regular expressions. The language designed to look like cartoon swearing.

Cheeso
+2 if I could for cartoon swearing.
kenny
A: 

Here is the best ever answer on your question. It have 2302 votes up.

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

Vasiliy Borovyak