2

I have a string like

string html = "truongpm<b><i>bold italic</i></b><b>bold</b><i>italic</i>";

How do i get array like

a[0] = "truongpm", a[1]= "<b><i>bold</i></b>", a[2]="<b>bold</b>", a[3]="<i>italic</i>"

from this string. Now i use this code

string tagRegex = @"<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>";
MatchCollection matchesImgSrc = Regex.Matches(html, tagRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
        foreach (Match m in matchesImgSrc)

But it just get

a[0]= "<b><i>bold</i></b>", a[1]="<b>bold</b>", a[2]="<i>italic</i>"

there are no "truongpm" Please help me! Thank

1
  • Put [^<>]+| before your current pattern. Commented Mar 19, 2015 at 7:48

2 Answers 2

2

Here is the code you can use:

var l = new List<string>();
var html = "truongpm<b><i>bold italic</i></b><b>bold</b><i>italic</i>";
var tagRegex = @"[^<>]+|<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>";
var matchesImgSrc = Regex.Matches(html, tagRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match m in matchesImgSrc)
    l.Add(m.Value);
Sign up to request clarification or add additional context in comments.

Comments

1

Your RegExp only matches strings within tags. If you want to capture strings without any tag too, you must add an alternative to your regular expression. This can be done by adding ([^<>]+) so that your expression would look like ([^<>]+)|{your existing expression}. On Websites like Regex Pal you find assistance in creating regular expressions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.