Community
    • Login

    Search & Replace help "Swap numbers in middle of code"

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    8 Posts 3 Posters 25 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.
    • Ricardo BarrosoR
      Ricardo Barroso
      last edited by

      Hello,

      I am trying to mod a game where i have text files in which I need to replace hundreds of lines of this type :

      …
      RequiredItem(“Craft_Scrap”, 35);
      RequiredItem(“Craft_Rags”, 12);
      RequiredItem(“Craft_Leather”, 12);
      RequiredItem(“Craft_Blades”, 12);
      …
      …
      AlternativePrice(“Craft_Rags”, 20);
      AlternativePrice(“Craft_Scrap”, 40);
      AlternativePrice(“Craft_Blades”, 8)
      …
      …
      RepairPart(“Craft_Scrap”, 18);
      RepairPart(“Craft_Battery”, 1);

      So i need to change all the “numerical” values to 0 (Zero). Those values can range from 1 to 5 digits (from what i saw so far).

      The end result for what i wanted, would be something like this:

      RequiredItem(“Craft_Scrap”, 0);
      AlternativePrice(“Craft_Rags”, 0);
      RepairPart(“Craft_Scrap”, 0);

      I think what complicates is that the codes has spaces, commas, etc etc. So all examples i saw in the web got nullified by regex / regular expressions errors

      Any kind of help that can be provided, i would apreciated if you could keep it simple. Meaning i dont mind doing one type of data at a time (1st do “RequiredItem(“Craft_Scrap”, 35);” and then RequiredItem(“Craft_Blades”, 12); … etc, etc).

      Since i already tried to learn regex and just got lost, so keeping it simple helps my Mind learn how you did it.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Ricardo Barroso
        last edited by PeterJones

        @Ricardo-Barroso said in Search & Replace help "Swap numbers in middle of code":

        I think what complicates is that the codes has spaces, commas, etc etc. So all examples i saw in the web got nullified by regex / regular expressions errors

        Regular expressions handle that just fine. You just have to include it.

        I am going to assume the rule you are implementing is "comma followed by zero or more horizontal spaces, followed by sequence of digits, followed by zero or more spaces, followed by ); needs to replace the number with 0.

        • \h means “horizontal space” (tab, space, etc)
        • \d means “digit”
        • * means “0-or-more”
        • + means “1-or-more”
        • putting things in the FIND in parentheses puts it in a numbered group for use in the replacement (so the first paren can replace that match with ${1} in the replacement, the second with ${2}, and so on)
        • because parens have meaning, if you want to match the literal ) character, it needs to be \) in the regex
        • more details in the references below

        Thus,
        FIND = (,\h*)\d+(\h*\);)
        REPLACE = ${1}0${2}
        SEARCH MODE = Regular Expression
        REPLACE ALL

        changes

        RequiredItem("Craft_Scrap", 35);
        RequiredItem("Craft_Rags", 12);
        RequiredItem("Craft_Leather", 12);
        RequiredItem("Craft_Blades", 12);
        ...
        ...
        AlternativePrice("Craft_Rags", 20);
        AlternativePrice("Craft_Scrap", 40);
        AlternativePrice("Craft_Blades", 8)
        ...
        ...
        RepairPart("Craft_Scrap", 18);
        RepairPart("Craft_Battery", 1);
        

        to

        RequiredItem("Craft_Scrap", 0);
        RequiredItem("Craft_Rags", 0);
        RequiredItem("Craft_Leather", 0);
        RequiredItem("Craft_Blades", 0);
        ...
        ...
        AlternativePrice("Craft_Rags", 0);
        AlternativePrice("Craft_Scrap", 0);
        AlternativePrice("Craft_Blades", 8)
        ...
        ...
        RepairPart("Craft_Scrap", 0);
        RepairPart("Craft_Battery", 0);
        

        If you actually have more complicated requirements (ie, if some of the text matches that rule, but shouldn’t be matched), then you need to give examples of things that shouldn’t match, not just ones that should match.

        Notice that your data was missing a semicolon on the Craft_Blades, so it didn’t get transformed, and still says 8. I assumed it was really missing the semicolon, and that since it didn’t match the same pattern, it should not transform. If that’s not the rule you want, you will have to be more specific about what your rule is, because you gave us a long list of things to search for, but then only showed us three examples of replacements, so we cannot read your mind on what the others should be.

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • Formatting Forum Posts
        • Notepad++ Online User Manual: Searching/Regex
        • FAQ: Where to find other regular expressions (regex) documentation
        Ricardo BarrosoR 1 Reply Last reply Reply Quote 0
        • Ricardo BarrosoR
          Ricardo Barroso @PeterJones
          last edited by

          @PeterJones

          the missing semicolon, was most likely cause of copy past in a rush.

          Anyway, if i understood correctly you are just trying to match Numbers on the right of the “,”

          And no that wasnt really what i wanted and it will cause errors. Basically the code i posted are extracts in a bigger code.

          So eventually, there will be lines that i dont want to change.
          For example:
          TokenPrice(EFaction_Exploration, 1);

          Let me put here a bit of a block of the code as it might help describe it.
          and sorry in advance as i dont know how to do tags to hide/show to avoid long blocks.

              Item("Craftplan_PipeBomb_FT_T2", CategoryType_Collectable)
              {
                  Name("&Craftplan_PipeBomb_FT_T2_N&");
                  Description("&Craftplan_PipeBomb_FT_T2_D&");
                  ItemType(ItemType_CraftPlan);
                  CraftplanType("UseItem");
                  Price(1);
                  Mesh("blueprint.msh");
                  Skin("default");
                  RequiredItem("Craft_Container", 0);
                  RequiredItem("Craft_Electrical_Parts", 0);
                  RequiredItem("Craft_Alcohol", 0);
                  RequiredItem("Craft_Battery", 0);
                  Color(Color_Blue);
                  CraftedItem("Throwable_PipeBomb_FT_T2", 1, 2);
                  HudIcon("blueprint_b");
                  AlternativePrice("DLC_FT_UpgradeComponent_T1", 0);
                  AlternativePrice("Craft_Container", 0);
                  AlternativePrice("Craft_Alcohol", 0);
                  AlternativePrice("Craft_Electrical_Parts", 0);
                  AlternativePrice("Craft_Battery", 0);
                  RequiredItemToShowInShop("Craftplan_PipeBomb_FT_T1");
                  ItemLevel(2, 4);
                  NextLevelBlueprintName("Craftplan_PipeBomb_FT_T3");
                  CraftingSound("dlc_ft_menu_craft_item_metal_success");
                  CraftingSoundStart("dlc_ft_menu_craft_item_metal_start");
                  GameVersion(1);
                  UID("7523131455520691382");
              }
          
          Terry RT 1 Reply Last reply Reply Quote 0
          • Terry RT
            Terry R @Ricardo Barroso
            last edited by Terry R

            @Ricardo-Barroso

            Your request for help is lacking in clarity. Note you were asked whether some fields weren’t to be updated but you haven’t answered that.

            There is a FAQ post here which outlines what is needed to get the help you desire. I’d suggest reading that first, then come back with an updated request with more clarity.

            Terry

            PS the link I provided was also in the Useful references from @PeterJones

            1 Reply Last reply Reply Quote 1
            • Ricardo BarrosoR
              Ricardo Barroso
              last edited by

              Just wanted to add that in the end i m only interest in the sections of code that starts with “RequiredItem”.

              Ricardo BarrosoR PeterJonesP Terry RT 3 Replies Last reply Reply Quote 1
              • Ricardo BarrosoR
                Ricardo Barroso @Ricardo Barroso
                last edited by PeterJones

                @PeterJones,

                Let me start by saying that this is my 1st post asking about coding, so obvious that requires some kind of clarity that i m not used. So please dont be offended as its not intentional or by lack of trying.

                Moving on and trying to be more “Precise”.

                In the example block of code that i showed (that was already changed when it Shouldn’t have been) it contains sections with:

                RequiredItem(“Craft_Electrical_Parts”, 50)

                And i only want to change the Numerical value at the end (might be number 1 or 5000).

                Inside the code the term “RequiredItem” is fixed but in front of it there is definition of the material used that is not fixed.
                So it could appear as

                RequiredItem(“Craft_Rags”, 7);
                or
                RequiredItem(“Craft_Alcohol”, 12);
                or another material not in the example.
                But again i only want to change the numerial value at the end (and preserving the space in front of the comma)

                Everything else in the code (and in the block of code i posted) i want to preserve as it is.

                I hope this is more helpful.

                1 Reply Last reply Reply Quote 0
                • PeterJonesP
                  PeterJones @Ricardo Barroso
                  last edited by PeterJones

                  @Ricardo-Barroso ,

                  First, sorry for the confusing post order. The approval queue wasn’t letting us approve the longer post, so I had to use an admin trick to re-create it.

                  Your short post has the most useful piece of info so far. I am off my pc for the day, but knowing that you only want things changed in a specific region changes the regex. If you could help define the start and end of the section rigorously, someone will be able to help you use a formula to get it to work

                  1 Reply Last reply Reply Quote 1
                  • Terry RT
                    Terry R @Ricardo Barroso
                    last edited by Terry R

                    @Ricardo-Barroso
                    So this may serve your needs. I am making some assumptions from the limited information you provided and actually your last post is somewhat contradictory to your first. I shall expand on that later.

                    So you mentioned regular expressions (regex) I am hoping you might be able to understand what my solution is doing. As we always suggest, start slowly, by finding and replacing in single line mode, before unleashing the regex on an entire file with the “Replace All” option.

                    In the Replace function we have:
                    Find What:(RequiredItem\("[^"]+", *)(\d+)
                    Replace With:${1}0

                    Make sure the search mode is “regular expression” and I’d suggest making sure the cursor is at the very start of the file, before clicking on the “Replace” button. Do the “Replace” option first, checking what each line was replaced with, before clicking on the “Replace All” once happy with the result.

                    So talking to the differing information you provided, you had alluded to wanting to achieve this on “some” different lines, but possibly “not all” lines. That was the clarity we needed to know. In the short post you stated only the “RequiredItem” lines, hence my solution here.

                    My solution will, as I say, only work on certain lines. You could easily replace the “RequiredItem” text with one from another line you want to do further replacements. If so, then again run it in single line mode to make sure it still works for you.

                    I will try to describe a part of the regex for you, specifically "[^"]+". It starts with the double quote first, then the [ and ] refer to a group of characters contained within, albeit the first one here is a ^, which is special and means “NOT the following character”. So basically it means select another character as long as it is NOT a double quote. The trailing + means as many as possible of the preceding group. Then lastly we have the trailing double quote.

                    So have a go, and if you do have more questions or need further help please do ask. But please remember people here give their time freely and to have to go back and forth many times in an attempt to get more clarity will often turn us right off.

                    Terry

                    PS, I should say, the red text of the regex can be copied and pasted directly, that way no issues with bad typing

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