How do I replace only the first <br> tag across multiple files?
-
Search the forum for “find the last”. We’ve recently talked about how to do that.
-
Hello, @scott-nielson, @peterjones, @alan-kilborn and All,
@scott-nielson, you should not need to reverse the lines order to achieve some replacements !
So, could you, like your first sample posted, provide us, both :
-
An initial example of your code containing several consecutive blocks
<p ••••••</p>
-
The same expected blocks, after the modifications needed
We can agree to flag any
HTML
code, that is to be added, by the notation<CODE 1>
,<CODE 2>
, etc.Best Regards,
guy038
-
-
@guy038 I see that you are called the ReEx Guru here. Thanks for agreeing to help.
I want this block of text:-<p class=MsoNormal style='margin-bottom:12.0pt;line-height:normal'><span style='font-size:13.5pt;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-Self-conscious. SELFISH. Egotism. <br> <br> -Usually self-confident. Domineering. Anger over his mistakes. <br> <br> -No confidence</span></p> <p class=MsoNormal style='text-align:justify'><span style='font-size:13.5pt; line-height:115%;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-When carefully selected remedies fail to act especially in acute diseases it frequently rejuvenates powers of organism (in chronic disease - Psorinum). <br> <br> -Complaints that relapse</span></p> <p><span><br> <br> -blah</span></p> <p><span><br> <br> -bleeh</span></p> <p><span><br> <br> -bloh</span></p> <p><span><br> <br> -bluh</span></p>
to become like:-
<p class=MsoNormal style='margin-bottom:12.0pt;line-height:normal'><span style='font-size:13.5pt;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-Self-conscious. SELFISH. Egotism. <br>zxcvb <br> -Usually self-confident. Domineering. Anger over his mistakes. <br> <br> -No confidence</span></p> <p class=MsoNormal style='text-align:justify'><span style='font-size:13.5pt; line-height:115%;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-When carefully selected remedies fail to act especially in acute diseases it frequently rejuvenates powers of organism (in chronic disease - Psorinum). <br>zxcvb <br> -Complaints that relapse</span></p> <p><span><br>zxcvb <br> -blah</span></p> <p><span><br>zxcvb <br> -bleeh</span></p> <p><span><br>zxcvb <br> -bloh</span></p> <p><span><br>zxcvb <br> -bluh</span></p>
If I use the RegEx,
^[^/]+\K<br>
to find the last<br>
tag in each block, between<p..........</p>
and then replace it with,<br>qwerty
which is what I put in the “replace” field, it replaces it with<br>qwerty
, or rather adds thezxcvb
text just after the last<br>
tag in each block but I want to add thezxcvb
text just after the first<br>
tag in each block as seen in the example given above.
Please help me! -
@guy038 Sorry RegEx Guru, I want this block of text:-
<p class=MsoNormal style='margin-bottom:12.0pt;line-height:normal'><span style='font-size:13.5pt;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-Self-conscious. SELFISH. Egotism. <br> <br> -Usually self-confident. Domineering. Anger over his mistakes. <br> <br> -No confidence</span></p> <p class=MsoNormal style='text-align:justify'><span style='font-size:13.5pt; line-height:115%;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-When carefully selected remedies fail to act especially in acute diseases it frequently rejuvenates powers of organism (in chronic disease - Psorinum). <br> <br> -Complaints that relapse</span></p> <p><span><br> <br> -blah</span></p> <p><span><br> <br> -bleeh</span></p> <p><span><br> <br> -bloh</span></p> <p><span><br> <br> -bluh</span></p>
to become like:-
<p class=MsoNormal style='margin-bottom:12.0pt;line-height:normal'><span style='font-size:13.5pt;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-Self-conscious. SELFISH. Egotism. zxcvb<br> <br> -Usually self-confident. Domineering. Anger over his mistakes. <br> <br> -No confidence</span></p> <p class=MsoNormal style='text-align:justify'><span style='font-size:13.5pt; line-height:115%;font-family:"Verdana","sans-serif";mso-fareast-font-family: "Times New Roman";mso-bidi-font-family:"Times New Roman";color:black; mso-ansi-language:EN-US'>-When carefully selected remedies fail to act especially in acute diseases it frequently rejuvenates powers of organism (in chronic disease - Psorinum). zxcvb<br> <br> -Complaints that relapse</span></p> <p><span>zxcvb<br> <br> -blah</span></p> <p><span>zxcvb<br> <br> -bleeh</span></p> <p><span>zxcvb<br> <br> -bloh</span></p> <p><span>zxcvb<br> <br> -bluh</span></p>
that is, after the replacement, the text (in this case I’ve used
zxcvb
as an example) should be added before the<br>
tag that is found (the first<br>
tag in each block between<p.....</p>
). -
@Scott-Nielson said in How do I replace only the first <br> tag across multiple files?:
I want this block of text:-
Repeating your request isn’t going to get @guy038 to respond any faster. We here on the forum give our time freely, to help others. We aren’t tied to the forum 24hrs a day, we mostly have real jobs that pay.
So just wait and since @guy038 has offered to help you he will, just allow him time to do so. Patience is a virtue!
Terry
-
@Terry-R said in How do I replace only the first <br> tag across multiple files?:
Repeating your request
Technically, it wasn’t just a repeat. The text in the “replace with” changed from
<br>zxcvb
tozxcvb<br>
in multiple locations. I think the OP said “sorry” because he got the data wrong.Unfortunately, due to the historic manner in which this user “asks” for things, it was not unreasonable for you to assume that the OP was just being impatient. That’s too bad, when a slight change of phrasing, and more of a willingness to do the work themselves, would make a drastic improvement in that user’s questions.
For the problem at hand, if Guy doesn’t have a chance to solve it for you right away, I highly recommend you should look at the generic regular expression for finding text between two keywords that Guy details here, which would solve the “for each section” part of the question; and maybe doing a search for “find last” in the forum, which would solve the “find the last
<br>
” part of the question. Combining the “find the last” with “for each section” would give you all that you need in order to figure it out on your own. And doing so would greatly benefit you, both in personal learning and showing us that you are willing to learn.I’ll give my generic advice again. Maybe you will read and understand it this time, and will start reading the pages linked
----
Please note: This Community Forum is not a data transformation service; you should not expect to be able to always say “I have data like X and want it to look like Y” and have us do all the work for you. If you are new to the Forum, and new to regular expressions, we will often give help on the first one or two data-transformation questions, especially if they are well-asked and you show a willingness to learn; and we will point you to the documentation where you can learn how to do the data transformations for yourself in the future. But if you repeatedly ask us to do your work for you, you will find that the patience of usually-helpful Community members wears thin. The best way to learn regular expressions is by experimenting with them yourself, and getting a feel for how they work; having us spoon-feed you the answers without you putting in the effort doesn’t help you in the long term and is uninteresting and annoying for us.
----
Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the
</>
toolbar button or manual Markdown syntax. To makeregex in red
(and so they keep their special characters like *), use backticks, like`^.*?blah.*?\z`
. Screenshots can be pasted from the clipboard to your post usingCtrl+V
to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries. -
@PeterJones said in How do I replace only the first <br> tag across multiple files?:
Technically, it wasn’t just a repeat. The text in the “replace with” changed from <br>zxcvb to zxcvb<br> in multiple locations. I think the OP said “sorry” because he got the data wrong.
So apologies to @Scott-Nielson , I did check the “before” text which was exactly the same, so assumed the “after” would also be.
@PeterJones we seem to have an influx of OPs whose posts are very sparsely populated. Meaning bugger all text! I’ve given up trying to intepret some of the posts, and I wonder why you still persist. Maybe you need another boilerplate answer (or expand the current one, which is getting bigger by the day?) to say “please expand on your question or else you will get limited/no responses”.
Terry
-
Hi, @scott-nielson, @peterjones, @alan-kilborn, @terry-r and All,
Why not this S/R :
SEARCH
(?s-i)<p.*?><span.+?\K<br>
REPLACE
zxcvb$0
As usual,
Regular expression
mode,Wrap around
option and just1
click on theReplace All
button ( Do not use theReplace
button, due to the\K
syntax ! )
Notes :
-
In SEARCH :
-
First, the
(?s-i)
syntax are in-line modifiers which means that :-
The regex dot char
.
matches any single character, even EOL ones, allowing multi-lines search -
The search is sensible to case ( not insensible ! )
-
-
The
.*?
and.+?
syntaxes represent, respectively, the shortest, possibly null, range of chars between the<p
and><span
strings and the shortest non-null range of chars between the><span
and first<br>
strings -
Then, the
\K
feature resets the present match. So, the final match is just the<br>
tag
-
-
In REPLACEMENT :
- The
$0
syntax represents the overall match, so the string<br>
, with this exact case
- The
Best Regards,
guy038
-
-
@guy038 Terrific! You are great! I was sensible enough to figure out that by, “
sensible to case
” you mean, “case sensitive
” and by, “The $0 syntax represents the overall match, so the string <br>, with this exact case
” you mean, “The $0 syntax represents the overall match, which is the string <br>, with this exact case
”. You must be French.Merci beaucoup
for your help! I learnt French some 30 years ago butje ne peux pas parler beaucoup de français
simply because I seldom bumped into French speaking people later. -
@Terry-R No problem!
-
@PeterJones
@Scott-Nielson said in How do I replace only the first <br> tag across multiple files?:@Alan-Kilborn or anyone else. I am doing Edit->Line Operations->Reverse line order, then in the RegEx mode,
Find:^[^/]+\K<br>
to find the last (which is actually the first since the lines are reversed)<br>
tag in each section and Replace:<br>zxcvb
,
Then Find:(?s)(?<=color:black'>)-(.*?) *(?=zxcvb)
and Replace:qwerty$1xxx
where qwerty and xxx are some HTML codes I wanna add but is there a RegEx that can help me do the needful without the Edit->Line Operations->Reverse line order (to find the first<br>
tag in each section)?By the above, you should have figured out that I do try to figure out how to do these things.
I seek a solution here only if I am unable to figure out what to do and will continue to do so - I ain't too proud to beg.
I thank you for the RegEx that helped me find the very first<br>
tag -(?s)\A.*?\K<br>
Thanks again! -
@PeterJones If you give me a link to check out something or give me a hint, I do check it out but luckily, @guy038 gave me the right solution (spoon feeding as you call it)!
-
Hi, @scott-nielson and All,
A bit off-topic : Yeah, it’s a common mistake when doing translations ! In French, this kind of words are known as “faux amis” words ! Just because :
-
In English, the adjective
sensible
have the French sense ofraisonnable
/sensé
orpratique
-
In French the adjective
sensible
have the English sense ofsensitive
ornoticable
So, I should have used the expression
sensitive to case
!Cheers,
guy038
-