1

It would be great if someone could provide me the Regular expression for the following string.

Sample 1: <div>abc</div><br>

Sample 2: <div>abc</div></div></div></div></div><br>

As you can see in the samples provided above, I need to match the string no matter how many number of </div> occurs.

If there occurs any other string between </div> and <br>, say like this <div>abc</div></div></div>DEF</div></div><br> OR <div>abc</div></div></div></div></div>DEF<br>, then the Regex should not match.

Thanks in advance.

1
  • Looks like a pain. It also depends on what part of the string you are trying to match. Just the text between the last <div> and it's closing </div>? Or the whole thing? And is the <div> allowed to have attributes? Are other elements allowed to have attributes? Are you sure you want to do this by regex? it doesn't look all too elegant (better, as others suggested use a parser instead?) Commented Oct 9, 2010 at 15:30

5 Answers 5

3

Try this:

<div>([^<]+)(?:<\/div>)*<br>

As seen on rubular

Notes:

  • This only works if there are not tags in the abc part (or anything that has a < symbol).
  • You might want to use start and end of string anchors (^<div>([^<]+)(?:<\/div>)*<br>$ if you want your string to match the pattern exactly.
  • If you want to allow the abc part to be empty, use * instead of +

That being said, you should be wary of using regex to parse HTML.

In this example, you can use regex because you are parsing a (hopefully) known, regular subset of HTML. But a more robust solution (ie: an [X]HTML parser like HtmlAgilityPack) is preferred when it comes to parsing HTML.

Sign up to request clarification or add additional context in comments.

3 Comments

@Dusty Or anything with a < for that matter.
@NUE - right. (sorry if it came out as dickish, I just wanted to point it out in case the op meant for abc to include more than plain text)
@Dusty Not at all. That actually should've been included in the post.
1

You need to use a real parser. Things like infinitely nested tags can't be handled via regex.

1 Comment

This is stuff that belongs in a comment.
1

You could also include a named group in the the expression, e.g.:

<div>(?<text>[^<]*)(?:<\/div>)*<br>

Implemented in C#:

var regex = new Regex(@"<div>(?<text>[^<]*)(?:<\/div>)*<br>");
Func<Match, string> getGroupText = m => (m.Success && m.Groups["text"] != null) ? m.Groups["text"].Value : null;
Func<string, string> getText = s => getGroupText(regex.Match(s));

Console.WriteLine(getText("<div>abc</div><br>"));
Console.WriteLine(getText("<div>123</div></div></div></div></div><br>"));

Comments

0

NullUserException's answer is good. Here are a couple of questions, and variations, depending on what you want.

Do you want to prevent anything from occurring before the open div tag? If so, keep the ^ at the beginning of the regex. If not, drop it.

The rest of this post refers to the following section of the regex:

([^<]+?)

Do you want to capture the contents of the div, or just know that it matches your form? To capture, leave it as is. If you don't need to capture, drop the parentheses from the above.

Do you want to match if there is nothing inside the div? If so change the + in the above to *

Finally, although it will work fine, you don't need the ? in the above.

Comments

0

I think, this regex is more flexible:

  <div\b[^><]*+>(?>.*?</div>)(?:\s*+</div>)*+\s*+<br(?:\s*+/)?>

I don't include the ^ and $ in the beginning and the end of my regex because we cannot assure that your sample will always in a single line.

2 Comments

Sure, if I didn't, I'll put it in a comment instead of an answer.
If it doesn't work, maybe your PCRE library didn't support possessive quantifiers. If so, try to remove every plus signs (+) after the asterisks from the regex. If that doesn't work too, remove the atomic grouping I made, that is, remove the (?> and its pair bracket. Good Luck!

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.