1

I want to find a regex formula as to select all line that does not have <p class="test_formal"> in front of <span class="test_formal2">

<p class="test_formal"><span class="test_formal2">

I made a regex, but is not too good.

(?s)\A(?!.*?(?<!\w)(<span class="test_formal2">)(?!\w)<p class="test_formal">).*

Can anyone help me a little bit?

1 Answer 1

2

This will match all lines that have:

<span class="test_formal2"> followed or not by <p class="test_formal">

But not <p class="test_formal"> followed by <span class="test_formal2">

  • Ctrl+F
  • Find what: ^(?:(?!<p class="test_formal">).)*<span class="test_formal2">.*$
  • check Wrap around
  • check Regular expression
  • UNCHECK . matches newline
  • Search in document

Explanation:

^                               # beginning of line
  (?:                           # start non capture group
    (?!                         # negative lookahead, make sure we haven't
      <p class="test_formal">   # literally
    )                           # end lookahead
    .                           # 1 any character
  )*                            # end group, may appear 0 or more times
  <span class="test_formal2">   # literally
  .*                            # 0 or more any character
$                               # end of line

Given:

<p class="test_formal">
<span class="test_formal2">
<p class="test_formal"><span class="test_formal2">
blah blah <p class="test_formal"><span class="test_formal2"> blah blah
<span class="test_formal2"><p class="test_formal">
blah blah <span class="test_formal2"><p class="test_formal"> blah blah

It matches:

Search "^(?:(?!<p class="test_formal">).)*<span class="test_formal2">.*$" (3 hits in 1 file)
  new 2 (3 hits)
    Line 2: <span class="test_formal2">
    Line 5: <span class="test_formal2"><p class="test_formal">
    Line 6: blah blah <span class="test_formal2"><p class="test_formal"> blah blah

Regex101 Demo

3
  • thank you, Toto. But there is a small problem. The regex should find only those line which necessarily contain <span class="test_formal2"> . So, your regex selects inverse, first of all those line that contains <p class="test_formal">. See this update regex101.com/r/YEcK8e/2
    – user706401
    Dec 30, 2018 at 15:05
  • ok, I find the problem. Update a little bit your regex, just add ! in fist paranthesis, right before <p class="test_formal">. Works. So, please update your regex answer with this ^(?:(?:.*?!<p class="test_formal">(?:(?!<span class="test_formal2">).)*)|(?:(?!<p class="test_formal">).)*<span class="test_formal2">.*)$
    – user706401
    Dec 30, 2018 at 15:33
  • @RobRob: That wasn't clear. I've completely rewrite the regex, it's now really simplifed.
    – Toto
    Dec 30, 2018 at 16:23

You must log in to answer this question.