• Login
Community
  • Login

Help with search and replace code

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
11 Posts 5 Posters 514 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.
  • J
    Jessica Bruno
    last edited by Jessica Bruno Jul 13, 2023, 11:01 AM Jul 13, 2023, 11:00 AM

    Hi,

    I’m modding some game files and hoping to do this in bulk as there are hundreds to do! The format of the files is as follows:

    name: "Medium 6x4"
    price: 26200
    unlock: 0
    

    How would I set this to change the ‘price’ (which could be any number from 1 to infinity) to 1, and the ‘unlock’ (which could be anything from 0 to infinity) to 0?

    Thanks

    A 1 Reply Last reply Jul 13, 2023, 11:37 AM Reply Quote 0
    • A
      Alan Kilborn @Jessica Bruno
      last edited by Jul 13, 2023, 11:37 AM

      @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
      • G
        guy038
        last edited by guy038 Jul 13, 2023, 12:22 PM Jul 13, 2023, 12:19 PM

        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

        A P 2 Replies Last reply Jul 13, 2023, 12:25 PM Reply Quote 0
        • A
          Alan Kilborn @guy038
          last edited by Jul 13, 2023, 12:25 PM

          @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
          • P
            Paul Wormer @guy038
            last edited by Paul Wormer Jul 13, 2023, 12:50 PM Jul 13, 2023, 12:46 PM

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

            1 Reply Last reply Reply Quote 0
            • G
              guy038
              last edited by Jul 13, 2023, 1:15 PM

              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

              J 1 Reply Last reply Jul 13, 2023, 2:09 PM Reply Quote 3
              • J
                Jessica Bruno @guy038
                last edited by Jul 13, 2023, 2:09 PM

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

                A 1 Reply Last reply Jul 13, 2023, 9:04 PM Reply Quote 0
                • A
                  Alan Kilborn @Jessica Bruno
                  last edited by Alan Kilborn Jul 13, 2023, 11:24 PM Jul 13, 2023, 9:04 PM

                  @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
                  • G
                    guy038
                    last edited by Jul 14, 2023, 1:23 AM

                    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

                    M 1 Reply Last reply Jul 14, 2023, 6:08 PM Reply Quote 2
                    • M
                      mkupper @guy038
                      last edited by Jul 14, 2023, 6:08 PM

                      @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
                      • G
                        guy038
                        last edited by guy038 Jul 15, 2023, 10:17 AM Jul 15, 2023, 3:14 AM

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