Community
    • Login

    How do I replace only the first <br> tag across multiple files?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    25 Posts 5 Posters 1.7k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Scott NielsonS
      Scott Nielson @PeterJones
      last edited by

      @PeterJones Thanks a lot man, you saved me a lot of time!
      Now, for the same block of text as posted right at the top, where I have this at the bottom/end:-

      <br>
          -Eyes</span></p>
      

      How do I find all of those, replace the <br> tag (the last <br> tag in that block) with something else, keep the text - in this case, “-Eyes (with the dash)” and add something after the </span></p>? I know that using a $1 in the “replace” field will keep the text - in this case, “-Eyes (with the dash)” and using *(?=</span></p>) at the end will keep the </span></p> string as it is, after the replacement.

      1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @PeterJones
        last edited by

        @PeterJones said in How do I replace only the first <br> tag across multiple files?:

        You are up to 6 regex questions in barely more than a month.

        7 now!

        Scott NielsonS 1 Reply Last reply Reply Quote 0
        • Scott NielsonS
          Scott Nielson @Alan Kilborn
          last edited by

          @Alan-Kilborn Is there a limit on the number of questions one can ask here? I’m trying my best to find a solution and ask for a solution here only if I no longer find a solution!

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @Scott Nielson
            last edited by

            @Scott-Nielson

            Did you even read Peter’s “Please note” section above?
            This forum is about Notepad++, not about how to transform data with regular expressions.
            Plus, you aren’t really asking questions about regular expressions.
            You’re basically saying “do my regular expression writing work for me”.
            As Peter attempted to explain, this is frowned upon here.
            The extent of regular expression questions here should be general questions, such as “Which regex engine does N++ use?” or of course any suspected bugs with something going on with a regex.
            Don’t become a regex “taker” (seen many times here).

            Scott NielsonS 1 Reply Last reply Reply Quote 1
            • Scott NielsonS
              Scott Nielson @Alan Kilborn
              last edited by Scott Nielson

              @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)?

              PeterJonesP Scott NielsonS 2 Replies Last reply Reply Quote 0
              • PeterJonesP
                PeterJones @Scott Nielson
                last edited by

                @Scott-Nielson ,

                Search the forum for “find the last”. We’ve recently talked about how to do that.

                1 Reply Last reply Reply Quote 1
                • guy038G
                  guy038
                  last edited by guy038

                  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

                  Scott NielsonS 2 Replies Last reply Reply Quote 0
                  • Scott NielsonS
                    Scott Nielson @guy038
                    last edited by

                    @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 the zxcvb text just after the last <br> tag in each block but I want to add the zxcvb text just after the first <br> tag in each block as seen in the example given above.
                    Please help me!

                    1 Reply Last reply Reply Quote 0
                    • Scott NielsonS
                      Scott Nielson @guy038
                      last edited by

                      @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>).

                      1 Reply Last reply Reply Quote 0
                      • Terry RT
                        Terry R
                        last edited by

                        @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

                        PeterJonesP 1 Reply Last reply Reply Quote 2
                        • PeterJonesP
                          PeterJones @Terry R
                          last edited by

                          @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 to zxcvb<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.

                          @Scott-Nielson ,

                          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 make regex 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 using Ctrl+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.

                          Scott NielsonS 1 Reply Last reply Reply Quote 2
                          • Terry RT
                            Terry R
                            last edited by

                            @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

                            Scott NielsonS 1 Reply Last reply Reply Quote 1
                            • guy038G
                              guy038
                              last edited by guy038

                              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 just 1 click on the Replace All button ( Do not use the Replace 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

                              Best Regards,

                              guy038

                              Scott NielsonS 1 Reply Last reply Reply Quote 0
                              • Scott NielsonS
                                Scott Nielson @guy038
                                last edited by Scott Nielson

                                @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 but je ne peux pas parler beaucoup de français simply because I seldom bumped into French speaking people later.

                                1 Reply Last reply Reply Quote 1
                                • Scott NielsonS
                                  Scott Nielson @Terry R
                                  last edited by

                                  @Terry-R No problem!

                                  1 Reply Last reply Reply Quote 0
                                  • Scott NielsonS
                                    Scott Nielson @Scott Nielson
                                    last edited by Scott Nielson

                                    @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!

                                    1 Reply Last reply Reply Quote 0
                                    • Scott NielsonS
                                      Scott Nielson @PeterJones
                                      last edited by Scott Nielson

                                      @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)!

                                      1 Reply Last reply Reply Quote 0
                                      • guy038G
                                        guy038
                                        last edited by guy038

                                        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 of raisonnable / sensé or pratique

                                        • In French the adjective sensible have the English sense of sensitive or noticable

                                        So, I should have used the expression sensitive to case !

                                        Cheers,

                                        guy038

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post
                                        The Community of users of the Notepad++ text editor.
                                        Powered by NodeBB | Contributors