Mefhisto1
Mefhisto1

Reputation: 2228

Regex to match all characters wrapped in a div tag

<div class="myClass">
<form method = "post" name="loginForm" >
<input type="text" name="userName"/>
</form>
<form method = "post" name="signupForm" >
<input type="text" name="userName"/>
</form>
</div>

if this was to be an example html.

<div>(.*?)</div>

So, something like this, anything (even nested tags) between opening and closing div tag

Upvotes: 1

Views: 2251

Answers (2)

pysco68
pysco68

Reputation: 1146

I had to do this a few years ago, but with additional flexibility (capturing content of no matter what element):

<(?<element>\w+).*?>(?<content>.*)?</[\s]*\1>

Which works quite okay, but I have to agree with the comments above; if you can, use a fully fledged DOM parser! It'll handle edge cases etc...

Not if you really just want to get content of <div />'s that would be:

<(div).*?>(?<content>.*)?</[\s]*\1>

Upvotes: 0

A.A
A.A

Reputation: 1148

Perhaps help you :

(<[div]+\s[a-zA-Z]+\=\"[a-zA-Z]+\">)(.*)(<\/[div]+>)

enter image description here

URL : https://www.regex101.com/

Upvotes: 1

Related Questions