regex: Find all lines starting with a specific tag and ending with a different tag
-
I want to Find all lines starting with a specific tag and ending with a different tag. For example:
<p class=“amigo”>My mother is at home.<br>
tried a regex, but doesn’t work to good, because the selection does not stop at <br>
.*<p class="amigo">(?s)(.*)<br>*$
Can anyone help me?
-
@Robin-Cruise I’m no expert, but maybe you need to escape the
<
and>
characters:
.*\<p class="amigo"\>(?s)(.*)\<br\>*$
-
you are not an expert :) did you try your solution before writing it here?
no, it’s not about escape. My regex is almost good, but something else is missing :)
-
by the way, I just find the solution:
.*<p class="amigo">(?s)(?-s)(.*)<br>*$
see here:
-
The sequence (?s)(?-s) makes little sense in a regular exp. I think what you really wanted was to drop both of those and change .* to .*?
-
The reason why adding the
(?-s)
worked for you: originally, you had set “dot matches newline” using(?s)
, so your(.*)
greedily matched everything (including newlines) from start of the first amigo-paragraph to the last line-ending-<br>
that it found. By turning off “dot matches newline” with(?-s)
, then it would only match when the<br>
occurs on the same line as the amigo-paragraph.A couple of nitpicks with your solution:
-
(?s)
and(?-s)
are opposites: by having one followed by the other, this turns on “dot matches newline” then immediately turns off “dot matches newline”. You just need(?-s)
to turn it off. -
<br>*$
: this matches the literals<
,b
,r
, followed by 0 or more occurrences of>
, followed by end of line. Which means it would match<br
, or<br>>>>>>>>>
, not just<br>
. -
>*$
: Also, because of the$
anchor immediately after the 0-or-more>
, then nothing – no whitespace, no nothing – will be allowed after the<br>
at the end of the line. I hope that’s what you intended. If you really meant to match it, even when more stuff comes after the<br>
on the same line, then you should use.*<p class="amigo">(?-s)(.*)<br>.*$
-
.*<p
: just a note: if you happen to have the☑ . matches newline
option checked in your Find window, then it will match from the beginning of the first line to the end of the last line with amigo-paragraph and<br>
. This can be seen with the example data here:<p class="amigo">My mother is at home.<br> asfa asffa <p class="amigo">My mother is at home.<br> asfa asffa <p class="amigo">My mother is at home.<p> afasf <p class="amigo">My mother is at home.<br> asfa asffa <p class="amigo">My mother is at home.<p> asdas <p class="amigo">My mother is at home.<br> asfa asffa
… where it would find 1 occurrence, when there are really four. To fix this, move the
(?-s)
to the beginning of the pattern, instead of after the amigo-paragraph.
My recommended regular expression, which will allow text after the
<br>
, will not match<br
, and will keep the instances separate:(?-s).*<p class="amigo">(.*)<br>.*$
-
-
@Alan-Kilborn beat me to the
(?s)(?-s)
… but if you just drop both, then<p class="amigo">My mother is at home.<p> asdas <p class="amigo">My mother is at home.<br> asfa asffa
will match from the first p-amigo line to the end of the
...<br>
line.
The examples I showed will correct that, too.edit: this is true only if dot-matches-newline checkbox is turned on in the dialog, of course… if it’s turned off, then it doesn’t matter whether .* is greedy or not. (edits #2-n: clarified wording a couple times)
-
yes, nice Peter your solution
(?-s).*<p class="amigo">(.*)<br>.*$
Ok, but if I want to make this search and replace just the
<br>
from all those lines, how can I do this?So, to find all lines that contains those 2 tags, and replace only tha last one,
<br>
with</p>
Something like this, but not too good:Search:
(?-s).*<p class="amigo">(.*)(<br>).*$
Replace by:
1\2\3</p>
Can you help me a little bit?
-
@Robin-Cruise
When replacing you need to capture text that you want to return, this is achieved by using(
and)
around those portions. Then in the replace field you use the\1
,\2
etc to return them.
So for your need to replace the<br>
, we don’t need brackets around it, but around the other text.
Find what:(?-s)(.*<p class="amigo">.*)<br>(.*)$
Replace with:\1</p>\2
Note that the following portion (after the <br>) is now bracketed. Although none of the examples actually show any text following the <br>, if any did occur they would be captured and returned.
Terry
-
great, thanks. But please, so to understand better this replace on your regex :
(?-s)(.*<p class="amigo">.*)<br>(.*)$
so,
\1
is the first bracket
\2
second bracket
\3
third bracketbut, If I replace your regex just with
\1
it will delete<br>
So, seems to me that those\1 \2 \3 ..
selects the words/code which is not in brackets. Correct ? -
The regex
(?-s)(.*<p class="amigo">.*)<br>(.*)$
only has two capturing parenthesis-pairs. The(?-s)
is a non-capturing command to the regex engine, and doesn’t capture anything. The next parenthesized group is a capture group, and captures the...<p...>...
into\1
. The<br>
is not captured (it’s not inside parentheses). The next parenthesized group is a capture group, and grabs everything beyond the<br>
to the end of the line.(?-s)(.*<p class="amigo">.*)<br>(.*)$ ^^^^^ = not a capture group (it's a command to regex) (?-s)(.*<p class="amigo">.*)<br>(.*)$ ^^^^^^^^^^^^^^^^^^^^^^^ = first capture group, into \1 (?-s)(.*<p class="amigo">.*)<br>(.*)$ ^^^^ = not in parentheses, so not captured (?-s)(.*<p class="amigo">.*)<br>(.*)$ ^^^^ = second capture group, into \2
-----
This FAQ gives lots of good pointers to regex documentation. You can study more about regular expressions and capture groups vs. non-capture groups in the links provided there. -
@Robin-Cruise said:
But please, so to understand better this replace on your regex
I think you misunderstand the brackets around the
?-s
. This is NOT a capture group, it is a modifier (I think that’s the correct wording). It modifies the parameters that the regex uses when searching for text. The first bracket for capturing is the one for the opening tag sequence(.*<p class="amigo">.*)
.Might I suggest you need to read up a bit more on what metacharacter sequences are used in regex to modify the parameters. This site I use often and has good examples.
http://rexegg.com/regex-quickstart.html
Bear in mind that most regex engines will be slightly different in how they use these metacharacters so not everything on this site will fit exactly to how Notepad++ works, but nonetheless the information is still helpful.Terry
-
Oh, I think I just realized your confusion. @Terry-R used the word “bracketed” to mean “surrounded by parentheses
(...)
”, but you interpreted it to mean “surrounded by angle brackets<...>
”.Given different people’s terminology in reference to parentheses/parenthesis, braces, brackets, curly brackets, square brackets, angle brackets, I try to be as explicit as possible, and often give examples of which I mean (though I sometimes fail at this).
-
Regexr.com calls
(?-s)
a “mode modifier”.PCRE (Perl Compatible Regular Expresions) have their origin in Perl, and I learned my regex through Perl, so when I’m confused, I go to Perl’s perlre manpage. That shows a fully-expanded non-capturing group with enabled modifiers, disabled modifiers, and a non-capturing pattern:
(?adluimnsx-imnsx:pattern)
adluimnsx
are the possible pattern modifiers to enable (where you can have 0 or more of the modifiers),-imnsx
are the possible pattern modifiers to disable (where you can have 0 or more of the modifiers after the-
),pattern
is the part of the regex pattern that you want to group and match, but not capture.- If you don’t have modifiers, it shortens to
(?:pattern)
; - if you don’t have a pattern and just want modifiers, it shortens to
(?adluimnsx-imnsx)
.
-
Sorry @Robin-Cruise Peter explained the confusion well.
@PeterJones said:@Terry-R used the word “bracketed” to mean…
Yes I meant the “round brackets”
(
and)
. As Peter stated there are SO many different varieties it’s very easy to get confused. Your regex101 link above would have shown you what each capture group referred to and there were the “round brackets” mentioned.I would strongly suggest you study the various characters used that have special meaning, following the links above that Peter and myself mentioned. Unless you get these basics sorted you will have ALL sorts of problems trying to create regexes successfully.
Terry
-
Be careful here. Notepad++ doesn’t use PCRE regular expressions, no mater what the N++ wiki says. It uses Boost regular expressions.
Of these:
(?adluimnsx-imnsx)
Boost does not support adlun, reducing what it does support to:
(?imsx-imsx)
If used one of the invalid ones, Notepad++ will say “Find: Invalid regular expression”
-
But I did learn something new from Peter’s post, that does work with Boost. I didn’t know that you could include a
:pattern
inside, for example,(?i)
. So it is perfectly legal to do this:(?-i)b(?i:b)b
to matchbbb
orbBb
but notBbb
,BBB
,bBB
, etc.Note that the
?i
only applies to what is inside the enclosing round brackets. After the closing one, the outer leading(?-i)
goes back into being in effect.Before this new knowledge I would have achieved the same thing this way:
(?-i)b((?i)b)b
or even messier(?-i)b(?:(?i)b)b
or(?-i)b(?i)b(?-i)b
Note also that
(?i:pattern)
is a non-capturing group. Andi
could bes
or whatever is legal (see prior post).Before some wiseguy points out that
b[bB]b
works just as well…these are just made up examples to show a technique, not true real-life searches. -
Continues to blah, blah, blah on and on…
So checking the Notepad++ regex wiki: http://docs.notepad-plus-plus.org/index.php/Regular_Expressions
I see that this syntax IS there, but I guess I never understood it because it is SO f*cked up. :-)
Here’s what the Wiki says:
(?:flags-not-flags ...), (?:flags-not-flags:...) Applies flags and not-flags to search inside the parentheses. Such a construct may have flags and may have not-flags - if it has neither, it is just a non-marking group, which is just a readability enhancer.
Here’s how I would write it so that it is (hopefully) understandable:
(?flags-notflags:searchpattern) Applies flags and notflags to the searchpattern inside the parentheses and forms a non-capturing group. Such a construct may optionally have flags and may optionally have -notflags ; if it has neither, it is just a simple non-capturing group. Note that the effect of flags/nonflags only applies to the searchpattern inside the enclosing parentheses.
-
Hi, all,
May be a bit late but here is a regex S/R which is able to detect and replace any ending tag, different of
</p>
, with this one, in any mono or multi-lines range<p class.....>.............<...>
, whatever its location on current lineSEARCH
(?s)<p class[^<]+\K(?!</p>)<(?-s).+>
REPLACE
</p>
Remark : I’s important to point out that, due to the
\K
syntax, this regex S/R works if you click on theReplace All
button , exclusively ! ( Any step-by-step replacement , with theReplace
button, will not work )So, assuming this test text :
bla bla <p class=“amigo”>My mother is at home.<br> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.<br> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.<h> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> ====== NOT CHANGED ====== bla blah bla bla <p class=“amigo”>My mother is at home.</h> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> ====== NOT CHANGED ====== bla blah bla bla <p class=“amigo”>My mother is at home.</a> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.<p> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.<br> bla bla bla blah
You would obtain :
bla bla <p class=“amigo”>My mother is at home.</p> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> ====== NOT CHANGED ====== bla blah bla bla <p class=“amigo”>My mother is at home.</p> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> ====== NOT CHANGED ====== bla blah bla bla <p class=“amigo”>My mother is at home.</p> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> bla bla bla blah bla bla <p class=“amigo”>My mother is at home.</p> bla bla bla blah
Best regards,
guy038
P.S. : To continue the @alan-kilborn discussion on flags, from the link below :
https://gammon.com.au/pcre/pcrepattern.html#SEC11
It is said :
Because alternative branches are tried from left to right, and options are not reset until the end of the subpattern is reached, an option setting in one branch does affect subsequent branches
So, for instance, the regex
(?-i)WEDNESDAY|(?i:friday|saturday|sunday)|Monday
would match :-
WEDNESDAY and Monday, in that exact case
-
Friday, as well as Saturday and Sunday in any case
-
-
Thanks everyone for help.
guyo38 made also a beautiful regex. But, there is a case where the formula does not fit. Suppose:
Case 1
<p class=“amigo”>1. Blah blah blah <br> 2. Blah blah blah <br> 3. Blah blah blah <br> 4. Blah blah blah </p>
Case 2
<p class=“amigo”>1. Blah blah blah <br> 2. Blah blah blah <br> 3. Blah blah blah <br> 4. Blah blah blah <br> new sentence here </p>
In this 2 cases your regex
(?s)<p class[^<]+\K(?!</p>)<(?-s).+>
replace by</p>
will replace the first instance of<br>
, and that will ruin the html code.So, in this 2 cases, I would only like to search and replace only those tags that contains only one instance of
<br>
. Or, if there are more<br>
, the replacement with</p>
would not take place anymore.