Community
    • Login

    Help find 2nd occurrence and replace

    Scheduled Pinned Locked Moved General Discussion
    16 Posts 5 Posters 5.6k 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.
    • Alan KilbornA
      Alan Kilborn @guy038
      last edited by

      @guy038

      But now (?-s) is no longer needed. :-)

      For readers that don’t know why that is true: It is only needed if a . appears in the regular expression.

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

        Hi, All,

        So, @Alan-kilborn has the last word !

        SEARCH (?=;\d\d:)

        REPLACE -TTT

        BR

        guy038

        1 Reply Last reply Reply Quote 2
        • astrosofistaA
          astrosofista @Alan Kilborn
          last edited by

          @Alan-Kilborn said in Help find 2nd occurrence and replace:

          Don’t forget your leading (?-s) or detail a setting for the . matches newline checkbox. I find the former easier.

          Oh, no, too much typewriting - it’s sunday morning :)

          Now, getting serious, you’re right on general cases, but in this particular one, given the data structure, that modifier has no effect at all. OP could check the . matches newline option but the output would be exactly the same.

          Of course, in other cases or situations it could do a lot of harm. Granted.

          Alan KilbornA 1 Reply Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @astrosofista
            last edited by

            @astrosofista said in Help find 2nd occurrence and replace:

            but in this particular one, given the data structure

            Yep, given the data structure…

            …but what if what the OP has shown is only part of a huge file, with vastly different types of data in different sections of the file? The user does a Replace All, verifies the section that they are currently looking at (cool, looks good!), doesn’t notice the other sections, saves, quits, they a few days later notices all of the unintended replacements in other sections of the file. Too late!

            It’s better to be safer, rather than potentially sorrier. My general rule: If I use a . in a regex, I always specify (?s) or (?-s) ahead of it, and ignore the . matches newline box.

            Of course, it is problem in that posters with questions of these type rarely give enough information to give foolproof solutions.

            Also, I’ve noticed that you’re “casual” about the . matches newline or (?s) / (?-s) specifications in other thread’s postings, when it is NOT as clear cut as this thread’s is. Just a word to “be careful” – this setting matters a lot when it comes to regex.

            astrosofistaA 1 Reply Last reply Reply Quote 1
            • astrosofistaA
              astrosofista @Alan Kilborn
              last edited by

              Hi @Alan-Kilborn

              As far as I can understand, you take a more protective attitude towards OP than me, maybe experience had taught you that. It’s fine and have nothing against it. However, in my case and in the absence of any compelling reason I try to respond as accurately as possible to the data exemplified, assuming that the posted example is representative of the data set to be processed. In other words, I am confident that OP made a correct analysis of the issue.

              Which is the best approach? Neither is unreasonable a priori, nor are wrong those who decided to take a more or less intermediate position. I suppose context would help to make better decisions - see my second example, below.

              Concerning the casual point, what I understand - and if I am wrong, please correct me - is that the modifier (?s) or its negated form has real significance when it is quantified, i.e. affected by * or +. I mean, given this data:

              Line1
              Line2
              Line3
              Line4
              

              a search expression as Lin.\d will get four matches, no matter what adjustment has been made - (?s) or (?-s). In this case it is useless to take precautions. However, if a quantifier is added, either * or +, then the regex in one match could find the whole set given the . matches newline is checked.

              Thus, in the last regex I posted, https://community.notepad-plus-plus.org/post/52389, in which the data provided was insufficient, I took due precautions to control the regex, changing the modifier as appropriate so that the regex would do the desired job correctly.

              Anyway, rest assured that my posts aim to help people, as far as my limited knowledge allows. And if any response of mine is not entirely adequate, it will have been by unintentional error, but never by malice.

              Alan KilbornA 1 Reply Last reply Reply Quote 2
              • Alan KilbornA
                Alan Kilborn @astrosofista
                last edited by

                @astrosofista said in Help find 2nd occurrence and replace:

                I am confident that OP made a correct analysis of the issue.

                I guess you haven’t read as many of these types of postings as I have.
                You’re predicating your subsequent statements on that “correct analysis”. :-)

                Sure the case of . by itself (not in a .*, .*?, .+ or .+? construct, and some others less frequently used) is fairly immune to trouble; for Windows files (which is what we talk about mostly, obviously) a single . shouldn’t be able to cause line-ending harm (it would take two . to match the \r\n).

                It’s okay, I was just trying to offer some friendly advice concerning .. I think it has been beat to death.

                rest assured that my posts aim to help people,

                Of course. I think you’ve been a strong contributor to date.

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

                  Hi, @astrosofista, @alan-kilborn and All,

                  Yes, at first sight, we could think that a single regex dot character, between two characters, is handled in a same way, whatever the state of the s in-line modifier and/or the state of the . matches newline option

                  But it’s not always the case :-( For instance, let’s suppose the 3-lines text, below, with Unix EOL, the last line without any EOL, and a space char between the last two c letters

                  cbcc1cc LF
                  ccZcc9cc LF
                  cc c

                  The regex (?s)c.c would match, successively, the 7 strings cbc , c1c , cLFc , cZc , c9c , cLFc and c c, so all the text !

                  Whereas the regex (?-s)c.c would match the 5 strings cbc , c1c , cZc , c9c and c c, only !


                  Therefore, it is best to remain precise and indicate the modifiers (?s) and/or (?-s), whenever necessary ;-)

                  Best Regards,

                  guy038

                  Alan KilbornA astrosofistaA 2 Replies Last reply Reply Quote 3
                  • Alan KilbornA
                    Alan Kilborn @guy038
                    last edited by

                    @guy038 said in Help find 2nd occurrence and replace:

                    But it’s not always the case :-( For instance, let’s suppose … Unix EOL

                    Which is exactly why I had said:

                    for Windows files

                    :-)

                    1 Reply Last reply Reply Quote 1
                    • astrosofistaA
                      astrosofista @Alan Kilborn
                      last edited by

                      @Alan-Kilborn said in Help find 2nd occurrence and replace:

                      It’s okay, I was just trying to offer some friendly advice concerning …

                      And I took it like this. What’s more, it’s even good advice, since I only considered the case of Windows, ignoring Unix and Mac.

                      Thank you, and I’ll keep that in mind.

                      Have fun!

                      1 Reply Last reply Reply Quote 1
                      • astrosofistaA
                        astrosofista @guy038
                        last edited by

                        Hi @guy038

                        Thank you, your’s is good advise, as well as @Alan-Kilborn’s. As I told him, I only took into consideration the Windows case, ignoring other platforms.

                        Will try to not forget it.

                        Best Regards

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