• Login
Community
  • Login

Help find 2nd occurrence and replace

Scheduled Pinned Locked Moved General Discussion
16 Posts 5 Posters 5.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.
  • C
    Cesar Silva
    last edited by Apr 12, 2020, 2:51 AM

    Hello everyone I am trying to find the 2nd occurence of ; and replace with TTT but I am not able to come up with one. I tried to run the on below but it’s not working for me. My line is:

    Text;Text;HH:MM;Text;;;
    Text;Text;HH:MM;Text;;
    Text;Text;HH:MM;Text;;

    and I wanted to be

    Text;Text-TTT;HH:MM;Text;;;
    Text;Text-TTT;HH:MM;Text;;
    Text;Text-TTT;HH:MM;Text;;

    Here is what I have tried
    ^((.+?;){4})(.+)$
    \1-TTT\3

    T 1 Reply Last reply Apr 12, 2020, 3:20 AM Reply Quote 0
    • T
      Terry R @Cesar Silva
      last edited by Terry R Apr 12, 2020, 3:21 AM Apr 12, 2020, 3:20 AM

      @Cesar-Silva said in Help find 2nd occurrence and replace:

      I am trying to find the 2nd occurence of ;

      Your regex got close I see. I edited it a bit and came up with the following rough solution. It works with your examples.
      Find What:^(.+?;.+?)(;.+)$
      Replace With:\1-TTT\2

      I used an online tester to get the solution as above. Try https://regex101.com/

      Cheers
      Terry

      1 Reply Last reply Reply Quote 2
      • G
        guy038
        last edited by guy038 Apr 12, 2020, 6:50 AM Apr 12, 2020, 6:48 AM

        Hello, @cesar-silva, @terry-R and All,

        An alternate solution could be :

        SEARCH (?-s);.+?(?=;..:)

        REPLACE $0-TTT

        Assuming there is only 1 colon, in each row, at the third position of the third HH:MM field, of course !

        Best Regards

        guy038

        A 1 Reply Last reply Apr 12, 2020, 3:33 PM Reply Quote 2
        • A
          astrosofista @guy038
          last edited by Apr 12, 2020, 3:33 PM

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

          An alternate solution could be

          Hi @guy038, All:

          Given that OP wants to get more an insertion than a replacement , wouldn’t be a ^ zero length match enough? I mean:

          Search: (?=;..:)
          Replace: -TTT

          Seems to work fine here.

          Best Regards

          1 Reply Last reply Reply Quote 3
          • A
            Alan Kilborn
            last edited by Apr 12, 2020, 3:40 PM

            @astrosofista

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

            All:

            I think maybe the OP attempted to “abstract” the data for us by using HH, MM, Text, etc…

            So perhaps an even better search term would be: (?=;\d\d:)

            “Better” as in : more restrictive to avoid changing unintended areas that just happen to match ;..:

            A 1 Reply Last reply Apr 12, 2020, 4:07 PM Reply Quote 1
            • G
              guy038
              last edited by Apr 12, 2020, 3:50 PM

              Hi, @cesar-silva, @terry-R, @astrosofista, @alan-kilborn and All,

              @astrosofista

              Very clever, indeed. You’are right about it : this look-ahead, alone, is enough to, precisely determine the position of the zero-length string ;-))

              @alan-kilborn

              Your remarks are pertinent, too !. Finally, the best regex S/R, regarding the OP, should be :

              SEARCH (?-s)(?=;\d\d:)

              REPLACE -TTT

              At least 4 people needed to reach the pantheon of regular expressions, pheew ;-))

              Cheers,

              guy038

              A 1 Reply Last reply Apr 12, 2020, 3:52 PM Reply Quote 3
              • A
                Alan Kilborn @guy038
                last edited by Apr 12, 2020, 3:52 PM

                @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
                • G
                  guy038
                  last edited by Apr 12, 2020, 4:04 PM

                  Hi, All,

                  So, @Alan-kilborn has the last word !

                  SEARCH (?=;\d\d:)

                  REPLACE -TTT

                  BR

                  guy038

                  1 Reply Last reply Reply Quote 2
                  • A
                    astrosofista @Alan Kilborn
                    last edited by Apr 12, 2020, 4:07 PM

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

                    A 1 Reply Last reply Apr 12, 2020, 5:48 PM Reply Quote 2
                    • A
                      Alan Kilborn @astrosofista
                      last edited by Apr 12, 2020, 5:48 PM

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

                      A 1 Reply Last reply Apr 12, 2020, 8:01 PM Reply Quote 1
                      • A
                        astrosofista @Alan Kilborn
                        last edited by Apr 12, 2020, 8:01 PM

                        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.

                        A 1 Reply Last reply Apr 12, 2020, 8:19 PM Reply Quote 2
                        • A
                          Alan Kilborn @astrosofista
                          last edited by Apr 12, 2020, 8:19 PM

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

                          A 1 Reply Last reply Apr 12, 2020, 11:05 PM Reply Quote 2
                          • G
                            guy038
                            last edited by guy038 Apr 12, 2020, 9:02 PM Apr 12, 2020, 9:01 PM

                            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

                            A A 2 Replies Last reply Apr 12, 2020, 9:20 PM Reply Quote 3
                            • A
                              Alan Kilborn @guy038
                              last edited by Apr 12, 2020, 9:20 PM

                              @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
                              • A
                                astrosofista @Alan Kilborn
                                last edited by Apr 12, 2020, 11:05 PM

                                @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
                                • A
                                  astrosofista @guy038
                                  last edited by Apr 12, 2020, 11:11 PM

                                  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
                                  7 out of 16
                                  • First post
                                    7/16
                                    Last post
                                  The Community of users of the Notepad++ text editor.
                                  Powered by NodeBB | Contributors