• Login
Community
  • Login

How do I replace a particular sentence across multiple files?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
24 Posts 6 Posters 2.3k 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.
  • A
    Alan Kilborn @Scott Nielson
    last edited by Nov 15, 2022, 12:23 PM

    @Scott-Nielson said in How do I replace a particular sentence across multiple files?:

    if the sentence is across different lines

    Find: (?-is)^(Please)\R(E-mail\.+? treatment)
    Replace: ${1} ${2}

    S 2 Replies Last reply Nov 15, 2022, 3:28 PM Reply Quote 3
    • S
      Scott Nielson @Alan Kilborn
      last edited by Scott Nielson Nov 15, 2022, 3:35 PM Nov 15, 2022, 3:28 PM

      @Alan-Kilborn I have some like this:-
      Please
      E-mail
      ............
      treatment
      and some like this:-
      (a <p........> string)Please E-mail
      ............treatment
      How do I find (and replace ) both with one RegEx?

      S 1 Reply Last reply Nov 15, 2022, 6:25 PM Reply Quote 0
      • S
        Scott Nielson @Scott Nielson
        last edited by Scott Nielson Nov 15, 2022, 6:40 PM Nov 15, 2022, 6:25 PM

        I got a solution at www.regex101.com
        I had to put (?=Please\K\s*E-mail)\K([\S\s]*?treatment) in the “Find” field, select the Regular Expression mode and put ${1}${2} in the “Replace with” field with no space between ${1} and ${2}

        1 Reply Last reply Reply Quote 0
        • S
          Scott Nielson @Alan Kilborn
          last edited by Nov 15, 2022, 6:39 PM

          Thanks @Alan-Kilborn for the help. I don’t think I could have figured out that I had to use ${1}${2} in the “Replace with” field.

          J 1 Reply Last reply Nov 15, 2022, 7:28 PM Reply Quote 0
          • J
            José Luis Montero Castellanos @Scott Nielson
            last edited by José Luis Montero Castellanos Nov 15, 2022, 7:30 PM Nov 15, 2022, 7:28 PM

            @Scott-Nielson
            Good afternoon :
            There is an even shorter form, is the \ℕ in the replacement field: where ℕ is 1 digit from 1 to 9.

            Replace with:
            \1\2
            

            The forms ${1}${2}… ${ℕ} are useful when the capturing group exceeds 9 for example ${10} “two digits!”. But each reference uses two more characters.

            Success doing the test :¬)

            T P 2 Replies Last reply Nov 15, 2022, 7:37 PM Reply Quote 0
            • T
              Terry R @José Luis Montero Castellanos
              last edited by Terry R Nov 15, 2022, 7:37 PM Nov 15, 2022, 7:37 PM

              @José-Luis-Montero-Castellanos said in How do I replace a particular sentence across multiple files?:

              There is an even shorter form, is the \ℕ in the replacement field: where ℕ is 1 digit from 1 to 9.

              Whilst that is correct it also introduces some uncertainty, especially when someone new to regular expressions sees the “\1” form and also the “${10}” form. Also when you see “\10” it can lead to confusion, is it really only group 1 or group 10.

              I myself learnt on the “\1” form but have; like most seasoned regex creators on this forum; changed to the “${10}” form as one can be consistent across all solutions provided and it is unambiguous. Given the Replace field is certainly long enough to cater for even the MOST complex of replacements, saving a few characters doesn’t make up for the confusion it can sometimes create.

              Terry

              J 1 Reply Last reply Nov 16, 2022, 12:19 AM Reply Quote 4
              • P
                PeterJones @José Luis Montero Castellanos
                last edited by PeterJones Nov 15, 2022, 7:43 PM Nov 15, 2022, 7:38 PM

                @José-Luis-Montero-Castellanos ,

                The underlying Boost regex documentation does not actually guarantee that \ℕ will work in REPLACEMENT, as that syntax is only technically defined in the SEARCH syntax; that means that Boost regex would be within their rights to drop support for \ℕ in replacements at a whim, without any notification. As such, I argue against the usage of \ℕ in replacements.

                Further, while both $ℕ or ${ℕ} are documented both by Boost and in the Notepad++ User Manual, the regulars in the forum tend to recommend ${ℕ}, especially to newbies, because that way if the user ends up making a 10th group or beyond, they won’t have to change the notation that they learned. (And @Terry-R expounded on the ambiguities involved while I was typing this up, so I won’t go into any more on that.)

                1 Reply Last reply Reply Quote 3
                • J
                  José Luis Montero Castellanos @Terry R
                  last edited by José Luis Montero Castellanos Nov 16, 2022, 12:50 AM Nov 16, 2022, 12:19 AM

                  @Terry-R
                  Hello:
                  That was why I specified that the backslash \ is only used with numbers from 1 to 9 because it is understood that the escape \ character is only followed by a single character in a escape sequence. And that helps reinforce that basic RegEx knowledge. Not only functional or applicable in Npp or Boost but in a wider field.

                  I also think there are two or three more ways to do this task. I have only shown the "reduced" or "simplified" form.

                  And most of the time you do not get to accumulate many capturing groups that exceed 9, the most common is 2 or 3. In this help thread! = 2.

                  Did you do the test? Had success?

                  Anyway it is good, to share broad views :)

                  S 1 Reply Last reply Nov 16, 2022, 1:39 AM Reply Quote 0
                  • S
                    Scott Nielson @José Luis Montero Castellanos
                    last edited by Nov 16, 2022, 1:39 AM

                    @José-Luis-Montero-Castellanos The \1\2 works but I will stick to what has been advised above! Thanks a lot to all of you @Alan-Kilborn @José-Luis-Montero-Castellanos @Terry-R @PeterJones

                    S 1 Reply Last reply Nov 16, 2022, 1:42 AM Reply Quote 2
                    • S
                      Scott Nielson @Scott Nielson
                      last edited by Nov 16, 2022, 1:42 AM

                      Is this the best RegEx to use in this case: (?=Please\K\s*E-mail)\K([\S\s]*?treatment) ?

                      T 1 Reply Last reply Nov 16, 2022, 2:22 AM Reply Quote 1
                      • T
                        Terry R @Scott Nielson
                        last edited by Nov 16, 2022, 2:22 AM

                        @Scott-Nielson said in How do I replace a particular sentence across multiple files?:

                        Is this the best RegEx to use in this case: (?=Please\K\sE-mail)\K([\S\s]?treatment) ?

                        I applaud you for trying something, but it appears more like you have just added in stuff until you got it to work, rather than fully understand what it is you are creating. Do you understand what the \K does, or even the [\S\s]?

                        Your use of ${1} and ${2} in an earlier post suggests you think the lookahead is the first capture group, it is not.

                        I was doing a bit of work on your request, and although I haven’t yet completed it I will show you what I have thus far.

                        (?-s)(Please)(?:(\s|\R)+)(E-mail)(?:(\s|\R)+)(.+?(?:(\s|\R)+)?treatment)

                        Now it isn’t finished as I think you need to elaborate on exactly the formats your text will present as. From that a better regex can be produced. But my initial Find expression above covers these scenarios:

                        Please
                        E-mail
                        ..............
                        treatment
                        (a <p........> string)Please E-mail
                        ............treatment
                        (a <p........> string)Please
                        E-mail
                        ............treatment
                        

                        Terry

                        S 3 Replies Last reply Nov 16, 2022, 8:00 AM Reply Quote 2
                        • S
                          Scott Nielson @Terry R
                          last edited by Scott Nielson Nov 16, 2022, 8:19 AM Nov 16, 2022, 8:00 AM

                          @Terry-R I understand that a \K wil help stop finding/matching text or something else that follows at that point and \s* is to find what follows even if it is on the next line. The RegEx you gave just above is Greek to me but it does not find the very first instance of the text, Please
                          E-mail
                          blah blah blah blah blah blah blah
                          treatment
                          I hope you can help find/match that also.
                          I also observed that there are now 5 capture groups

                          1 Reply Last reply Reply Quote 0
                          • S
                            Scott Nielson @Terry R
                            last edited by Scott Nielson Nov 16, 2022, 5:00 PM Nov 16, 2022, 4:58 PM

                            @Terry-R \s and \S, equate to “match any whitespace” and “match any non-whitespace” respectively. That is, if you specify [\s\S], your regular expression will match any one character, regardless of what it is, and if you use [\s\S]* your regular expression will match anything.

                            1 Reply Last reply Reply Quote 0
                            • S
                              Scott Nielson @Terry R
                              last edited by Scott Nielson Nov 16, 2022, 6:19 PM Nov 16, 2022, 5:07 PM

                              @Terry-R

                              Please
                              E-mail
                              ..............
                              treatment
                              (a <p........> string)Please E-mail
                              ............treatment
                              (a <p........> string)Please
                              E-mail
                              ............treatment
                              

                              after replacement, should become:

                              <b>Please
                              E-mail
                              ..............
                              treatment</b>
                              (a <p........> string)<b>Please E-mail
                              ............treatment</b>
                              (a <p........> string)<b>Please
                              E-mail
                              ............treatment</b>
                              

                              That means, I want a <b> and </b> to be added on either side of the text I posted above, in this “reply” to you

                              P 1 Reply Last reply Nov 16, 2022, 6:24 PM Reply Quote 0
                              • P
                                PeterJones @Scott Nielson
                                last edited by Nov 16, 2022, 6:24 PM

                                @Scott-Nielson ,

                                You keep on changing your spec. At some point, you need to start learning how to do it yourself rather than asking a particular Community member to keep editing all the free regex they’ve already given you.

                                Give it a try. Read the docs. Ask specific questions if you don’t understand specific syntax.

                                ----

                                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.

                                ----

                                Useful References

                                • Notepad++ Online User Manual: Searching/Regex
                                • FAQ: Where to find regular expressions (regex) documentation
                                S 1 Reply Last reply Nov 16, 2022, 6:35 PM Reply Quote 0
                                • S
                                  Scott Nielson @PeterJones
                                  last edited by Scott Nielson Nov 16, 2022, 6:50 PM Nov 16, 2022, 6:35 PM

                                  @PeterJones I could only think of this RegEx: (?=Please\K\s*E-mail)\K([\S\s]*?treatment) which may or may not be the appropriate RegEx (but it works) - I asked if that is best which is why @Terry-R responded. I am waiting for his response. You may also reply. Like I said, I want to add <b> and </b> on either side of what is searched for.

                                  A T 2 Replies Last reply Nov 16, 2022, 6:43 PM Reply Quote 0
                                  • A
                                    Alan Kilborn @Scott Nielson
                                    last edited by Nov 16, 2022, 6:43 PM

                                    @Scott-Nielson

                                    Peter was trying to say that this is a place for giving you some hints in the right direction, not solving your exact problem for you, then you change the problem and someone solves that exact problem, then you change the problem yet again…

                                    For the regulars here, this gets boring fast, and they are less and less likely to provide the requested stuff. For example, I’m already very bored with this thread and will be “clicking thru” it as more posts are added.

                                    It’s about the power of learning, after you receive some pointers in the right direction. If you can show that you’ve learned something and have a nuancy follow-up question about something specific, that is well-tolerated.

                                    But continuously changing/growing the problem and expecting a ready answer for every tweak is probably not going to get you far.

                                    but it appears more like you have just added in stuff until you got it to work, rather than fully understand what it is you are creating

                                    That’s a problem, too.

                                    1 Reply Last reply Reply Quote 2
                                    • T
                                      Terry R @Scott Nielson
                                      last edited by Terry R Nov 16, 2022, 7:02 PM Nov 16, 2022, 7:00 PM

                                      @Scott-Nielson said in How do I replace a particular sentence across multiple files?:

                                      Like I said, I want to add <b> and </b> on either side of what is searched for.

                                      Like the other members, it is hard helping you if you keep changing the criteria. However I will oblige with 1 further solution.

                                      But first, you need to understand that while we will help, we won’t keep supporting every little change you make to the request. So before I provide a solution (as I see it) I need from you:

                                      1. Every type of variety of line format you are looking for.
                                      2. The required solution for each variety. Maybe this just means saying “I want the line(s) selected to be returned as is with the <b> at the start and </b> at the end”. At least that’s what your recent reply to my interim solution seems to state.

                                      And as we talk about my interim solution, I find that it does select the first of those 3 varieties. See this image:

                                      9cfcca21-ef76-40d5-9e0a-fb31643a8f6e-image.png

                                      Terry

                                      PS maybe as you saw it you weren’t changing the criteria, but you certainly weren’t telling us the whole story. Having to extract information is very hard and is another reason why we just give up helping some posters!

                                      1 Reply Last reply Reply Quote 2
                                      • G
                                        guy038
                                        last edited by guy038 Nov 16, 2022, 7:46 PM Nov 16, 2022, 7:36 PM

                                        Hello, @scott-nielson, @terry-r, @alan-kilborn, @peterjones and All,

                                        @scott-Nielson :

                                        Referring to your post, where you showed us your given BEFORE data and your expected AFTER data, I’ve got a very simple solution !

                                        So, given your INPUT text, below :

                                        Please
                                        E-mail
                                        ..............
                                        treatment
                                        (a <p........> string)Please E-mail
                                        ............treatment
                                        (a <p........> string)Please
                                        E-mail
                                        ............treatment
                                        
                                        I even ADDED this case :
                                        
                                        (a <p........> string)Please........E-mail............treatment..............
                                        
                                        • Open the Replace dialog ( Ctrl + H )

                                        • SEARCH (?s)Please.+?E-mail.+?treatment

                                        • REPLACE <b>$0</b>

                                        • Uncheck all BOX options

                                        • Check the Wrap around option

                                        • Check the Mach case option, if necessary

                                        • Select the Regular expression search mode

                                        • Click once on the Replace All button or several times on the Replace button

                                        You should get your expected OUTPUT text :

                                        <b>Please
                                        E-mail
                                        ..............
                                        treatment</b>
                                        (a <p........> string)<b>Please E-mail
                                        ............treatment</b>
                                        (a <p........> string)<b>Please
                                        E-mail
                                        ............treatment</b>
                                        
                                        I even ADDED this case :
                                        
                                        (a <p........> string)<b>Please........E-mail............treatment</b>..............
                                        

                                        Best Regards,

                                        guy038

                                        S T 2 Replies Last reply Nov 16, 2022, 7:49 PM Reply Quote 2
                                        • S
                                          Scott Nielson @guy038
                                          last edited by Nov 16, 2022, 7:49 PM

                                          Thanks a lot @guy038 - you made my efforts fruitful!

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