M
M
MasterCopipaster2022-03-30 10:55:02
Regular Expressions
MasterCopipaster, 2022-03-30 10:55:02

How to add a regular expression to search inside a tag?

Help me people, I can’t figure out how to write a rhyme.
I need to find everything that is between the tags
<div class="WYSIWYG articlePage"> </div>
https://regex101.com/r/3YMWU6/1
I also tried like this

<([^\s]+)[\s+]class="(.*?)articlePage"[^>]*>([\s\S]*)<\/\1>

But everything goes wrong if the desired tag is nested in another tag...
<div><div class="WYSIWYG articlePage"> </div></div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MasterCopipaster, 2022-03-30
@MasterCopipaster

In short, this is how it works

#<div\s+class="(.*?)articlePage">[^<>]*(<div[^>]*>(?:[^<>]*|(?1))*</div>)[^<>]*</div>#si

R
Rsa97, 2022-03-30
@Rsa97

Regular expressions are not well suited for parsing recursively nested structures.
To work with HTML, it is better to use specialized libraries/functions, such as Simple DOM Parser for PHP or AngleSharp for C#.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question