J
J
jasonOk2016-01-08 10:54:32
PHP
jasonOk, 2016-01-08 10:54:32

How to compose correctly?

Condition:
The text can have 2 blocks <div>in a row (each of them can contain any text),
but the main thing is that one should follow the other.

<div class="first">First</div><div class="second">Second</div>

The order must be respected (the first block will obviously be with the class "first") and if there is only one block <div class="first">First</div>then it is cut out, just like if there is only a div <div class="second">Second</div>it is cut out.
What regular expression to check if there are two blocks or not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeniy Trebin, 2016-01-08
@evgeniy_trebin

/<div\s+class="first"\s*>.*<\s*\/div>\s*<div\s+class="second"\s*>.*<\s*\/div>/

Ruby example
>> regex = /<div\s+class="first"\s*>.*<\s*\/div>\s*<div\s+class="second"\s*>.*<\s*\/div>/

>> s = %{<div class="first">First</div><div class="second">Second</div>}
>> regex =~ s
=> 0
т.е. вхождение с 0 символа

>> s = %{<div class="second">Second</div>}
=> "<div class=\"second\">Second</div>"
>> regex =~ s
=> nil
т.е. вхождения нет

>> s = %{<div class="first">First</div>}
=> "<div class=\"first\">First</div>"
>> regex =~ s
=> nil
т.е. вхождения нет

R
Robot, 2016-01-08
@iam_not_a_robot

Check this if there is no other div before it:
/<\/div>\n{0,}<div\sclass=\"second\">/ism

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question