Community
    • Login

    Help with search and replace code

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    11 Posts 5 Posters 494 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 @Jessica Bruno
      last edited by

      @Jessica-Bruno

      If you set the find Search mode to Regular expression, you can search for variable numbers with \d+; example: price: \d+

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

        Hello, @jessica-bruno, @alan-kilborn and All,

        To complete the @alan-kilborn’s answer, I would say

        SEARCH (?-i)(?=price: )\d+

        REPLACE 1

        and :

        SEARCH (?-i)(?=unlock: )\d+

        REPLACE 0

        And, of course, tick the Regular expression search mode

        Note : (?-i), at beginning of the regex, means that this regex is sensitive to case ( so NON-insensitive )

        Best Regards,

        guy038

        Alan KilbornA Paul WormerP 2 Replies Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @guy038
          last edited by

          @guy038

          I think the virtue of my answer is that the OP could possibly remember it as a technique, whereas your answer likely results in “mind blown”, won’t remember.

          1 Reply Last reply Reply Quote 2
          • Paul WormerP
            Paul Wormer @guy038
            last edited by Paul Wormer

            @guy038 Don’t you mean (?-i)(?<=price: )\d+ ?

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

              Hi, @jessica-bruno, @paul-wormer, @alan-kilborn and All,

              Oh… yes ! I was wrong and, in addition to Alan remark, my first post gave very poor information !

              So, the exact search regexes are, of course :

              • SEARCH (?-i)(?<=price: )\d+

              and

              • SEARCH (?-i)(?<=unlock: )\d+

              BR

              guy038

              Jessica BrunoJ 1 Reply Last reply Reply Quote 3
              • Jessica BrunoJ
                Jessica Bruno @guy038
                last edited by

                @guy038 said in Help with search and replace code:

                (?-i)(?<=price: )\d+

                Thank you very much, this worked perfectly.

                @Alan-Kilborn said in Help with search and replace code:

                @guy038

                I think the virtue of my answer is that the OP could possibly remember it as a technique, whereas your answer likely results in “mind blown”, won’t remember.

                I thank you for your belief that I could be even begin to understand any of the code involved with this! No chance!!

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

                  @Jessica-Bruno said in Help with search and replace code:

                  I thank you for your belief that I could be even begin to understand any of the code involved with this! No chance!!

                  😁

                  Maybe if I’d have pointed out that in \d+ the d stands for “digit”?

                  The \ before it makes the d “special” (otherwise how would the program know you wanted something other than a real d?).

                  The + after it means “one or more”.

                  So put it all together and \d+ means “one or more digits”.

                  See, not that hard, and possibly memorable?

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

                    Hello, @jessica-bruno, @paul-wormer, @alan-kilborn and All,

                    As @alan-kilborn, here are some more explanations on the regexes :

                    • They both search for, at least, one character, considered as a digit, but ONLY IF preceded by the string price: or the string unlock:, with this exact case, and followed by a space character

                    • The four syntaxes (?=aaaaa) and (?!bbbbb), as well as (?<=ccccc) and (?<!ddddd), are called look-arounds which allow you to search for any expression :

                      • ONLY IF followed by the aaaaa string

                      • ONLY IF NOT followed by the bbbbb string

                      • ONLY IF preceded by the ccccc string

                      • ONLY IF NOT preceded by the ddddd string

                    • Note that the strings aaaaa, bbbbb, ccccc and ddddd are NEVER part of the expression to search for, but must be true in order to produce a valid match of the whole regex expression !

                    BR

                    guy038

                    mkupperM 1 Reply Last reply Reply Quote 2
                    • mkupperM
                      mkupper @guy038
                      last edited by

                      @guy038 The not versions always seem deadly. For example (?!bbbbb) also matches between each of the letters within bbbbb and (?<!ddddd) also matches between the letters within ddddd.

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

                        Hello, @jessica-bruno, @mkupper and All,

                        @mkupper, you’re right but these negative look-arounds are not intended to be used on their own but rather with a part outside the look-around(s !

                        Refer to the two tables below.

                        • Copy the text of the first column in a new tab

                        • Use the Mark dialog to get the text matched by each of these 9 regexes, in a red/orange color ( tick the Purge for each search option ) !


                        The results should be as below :

                        •---------------------•------------------------•----------------------•----------------------•-----------------------•-----------------------•
                        |                     |         12345          |    12345(?=aaaaa)    |    12345(?!bbbbb)    |    (?<=ccccc)12345    |    (?<!ddddd)12345    | 
                        •---------------------•------------------------•----------------------•----------------------•-----------------------•-----------------------•
                        |   12345aaaaa        |           X            |          x           |          X           |                       |           X           |
                        |   12345bbbbb        |           X            |                      |                      |                       |           X           |
                        |   12345zzzzz        |           X            |                      |          X           |                       |           X           |
                        |   ccccc12345        |           X            |                      |          X           |           X           |           X           |
                        |   ddddd12345        |           X            |                      |          X           |                       |                       |
                        |   zzzzz12345        |           X            |                      |          X           |                       |           X           |
                        |   ccccc12345aaaaa   |           X            |          X           |          X           |           X           |           X           |
                        |   ccccc12345bbbbb   |           X            |                      |                      |           X           |           X           |
                        |   ccccc12345kkkkk   |           X            |                      |          X           |           X           |           X           |
                        |   ddddd12345aaaaa   |           X            |          X           |          X           |                       |                       |
                        |   ddddd12345bbbbb   |           X            |                      |                      |                       |                       |
                        |   ddddd12345zzzzz   |           X            |                      |          X           |                       |                       |
                        |   zzzzz12345aaaaa   |           X            |          X           |          X           |                       |           X           |
                        |   zzzzz12345bbbbb   |           X            |                      |                      |                       |           X           |
                        |   zzzzz12345zzzzz   |           X            |                      |          X           |                       |           X           |
                        •---------------------•------------------------•----------------------•----------------------•-----------------------•-----------------------•
                        
                        •---------------------•------------------------------•--------------------------------•--------------------------------•--------------------------------•
                        |                     |   (?<=ccccc)12345(?=aaaaa)   |    (?<=ccccc)12345(?!bbbbb)    |    (?<!ddddd)12345(?=aaaaa)    |    (?<!ddddd)12345(?!bbbbb)    |
                        •---------------------•------------------------------•--------------------------------•--------------------------------•--------------------------------•
                        |   12345aaaaa        |                              |                                |               X                |               X                | 
                        |   12345bbbbb        |                              |                                |                                |                                | 
                        |   12345zzzzz        |                              |                                |                                |               X                | 
                        |   ccccc12345        |                              |               X                |                                |               X                | 
                        |   ddddd12345        |                              |                                |                                |                                | 
                        |   zzzzz12345        |                              |                                |                                |               X                | 
                        |   ccccc12345aaaaa   |              X               |               X                |               X                |               X                | 
                        |   ccccc12345bbbbb   |                              |                                |                                |                                | 
                        |   ccccc12345kkkkk   |                              |               X                |                                |               X                | 
                        |   ddddd12345aaaaa   |                              |                                |                                |                                | 
                        |   ddddd12345bbbbb   |                              |                                |                                |                                | 
                        |   ddddd12345zzzzz   |                              |                                |                                |                                | 
                        |   zzzzz12345aaaaa   |                              |                                |               X                |               X                | 
                        |   zzzzz12345bbbbb   |                              |                                |                                |                                | 
                        |   zzzzz12345zzzzz   |                              |                                |                                |               X                | 
                        •---------------------•------------------------------•--------------------------------•--------------------------------•--------------------------------•
                        

                        Best Regards,

                        guy038

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