Community
    • Login

    another replacement request for help

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    40 Posts 4 Posters 3.4k 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.
    • Carlos J. Segnini R.C
      Carlos J. Segnini R.
      last edited by

      Hello Notepad gurus,
      I am (also) looking for help with replacing several items in a file. I have searched and read a few other regex threads and although they help, I still cant find the way to do it.
      My problem is that the data is unformatted. For example, my file may contain these two lines in different places
      <UWI>AERY B 1H</UWI>
      <UWI>ALEXANDER-WESSENDORFF 1 (SA) A3 A 3H</UWI>

      And I need to replace what is inside of the <UWI>__</UWI> using a table replacement (separated by tabs between the old and the new value)
      AERY B 1H Aery #B1H
      ALEXANDER-WESSENDORFF 1 (SA) A3 A 3H Alexander - Wessendorff 1 A3H

      So, on one hand, I have the advantage that the fields to replace are always within the <UWI>__</UWI> and also that I have the replacement table formatted to be separated by tabs.

      But I am too new with this, so I can’t find a way to do it.
      Any help?
      Thanks in advance!

      EkopalypseE 1 Reply Last reply Reply Quote 1
      • EkopalypseE
        Ekopalypse @Carlos J. Segnini R.
        last edited by

        @Carlos-J-Segnini-R

        At least for me it is not clear what you want to achieve.
        If you post data which is formatted, post it within a ~~~ block
        to avoid loosing the format.
        Provide before and after data which clearly shows the difference.
        In addition, what is a table replacement?

        1 Reply Last reply Reply Quote 0
        • Carlos J. Segnini R.C
          Carlos J. Segnini R.
          last edited by Carlos J. Segnini R.

          Thanks and sorry, it was clear in my mind, which is clearly not so clear :P

          Here are a couple snapshots:
          This is how the data looks. There are hundreds of different UWI’s that I need to replace
          f8d75456-7877-4573-b33a-4c102b08e781-image.png

          And this is what I meant by table replacement. It is simply the old name and the new name separated by a tab

          334215ac-afe1-4043-9c70-ace50514e5b6-image.png

          What I need is to go through the file (XML file by the way), find the string on the first column of the table and replace it by the string in the second column of the table.

          My issue is that the names (UWI, stands for Unique Well Identifier, which is basically the well name) are unformatted (Different lenghts, multiple characters, spaces, etc)
          58793823-ff69-42ea-88dd-35d9afe5223d-image.png

          I hope it is more clear now. Thanks

          1 Reply Last reply Reply Quote 2
          • EkopalypseE
            Ekopalypse
            last edited by

            I’m sure the regex gurus here are able to find a solution based on regex
            only but I, personally, would use the python script plugin and this script

            from Npp import editor1, editor2
            
            replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line)
            
            def replace_with(m):
                return replacements[m.group()]
            
            editor1.rereplace('(?<=<UWI>).+?(?=</UWI>)', replace_with)
            

            Having the data in both views like

            ef26dc76-89cf-4fbe-b8a2-4a414ce72743-image.png

            and running the script will result in, I hope, the expected result.

            e22aa8ca-f9c8-45df-980a-6ee3de6c90d7-image.png

            Carlos J. Segnini R.C 1 Reply Last reply Reply Quote 3
            • Carlos J. Segnini R.C
              Carlos J. Segnini R. @Ekopalypse
              last edited by

              @Ekopalypse thanks so much!
              I wish I could try it, but now I am banging my head to install the plugin, as the firewall is blocking my download… No luck today.

              EkopalypseE 1 Reply Last reply Reply Quote 0
              • EkopalypseE
                Ekopalypse @Carlos J. Segnini R.
                last edited by

                @Carlos-J-Segnini-R

                :-) those network admins … :-)
                I don’t know how strict your rules are but maybe a solution would be to download it from github directly?

                Carlos J. Segnini R.C 1 Reply Last reply Reply Quote 0
                • Carlos J. Segnini R.C
                  Carlos J. Segnini R. @Ekopalypse
                  last edited by

                  @Ekopalypse said in another replacement request for help:

                  @Carlos-J-Segnini-R

                  :-) those network admins … :-)
                  I don’t know how strict your rules are but maybe a solution would be to download it from github directly?

                  Yeah, got it now. Had to write my user and password a hundred times, but it is installed. I am testing it now

                  1 Reply Last reply Reply Quote 0
                  • Carlos J. Segnini R.C
                    Carlos J. Segnini R.
                    last edited by

                    @Ekopalypse I want to be like you when I grow up :)
                    It worked like a charm, and faster than any regex I would believe.
                    Thanks so much
                    Regards

                    1 Reply Last reply Reply Quote 3
                    • EkopalypseE
                      Ekopalypse
                      last edited by

                      I want to be like you when I grow up

                      LOL - be careful with your wishes sometimes they come true :-D

                      1 Reply Last reply Reply Quote 0
                      • Carlos J. Segnini R.C
                        Carlos J. Segnini R.
                        last edited by

                        haha thanks.
                        May I ask another question?
                        What if I needed to change another field as well?
                        For example in the image below:
                        39e1a109-e611-4d7d-a037-6a47ecf46181-image.png
                        I find the name Aery #B1H in the file but then I need to change the Welltype from Oil to Gas (Or viceversa).
                        In this case the I would not be replacing the searched word (Aery #B1H), but another field occurring later in the same entry (<Welltype>Oil to Gas)
                        Thanks again

                        EkopalypseE 1 Reply Last reply Reply Quote 0
                        • EkopalypseE
                          Ekopalypse @Carlos J. Segnini R.
                          last edited by

                          @Carlos-J-Segnini-R

                          I would think this is a find/replace action.
                          Find: <UWI>Aery #B1H</UWI>\R\h*<WellType>\K.+?(?=</WellType>)
                          Replace:Gas
                          tick regular expression

                          Because of the usage of the \K syntax you can only use replace all button.

                          Carlos J. Segnini R.C 1 Reply Last reply Reply Quote 2
                          • Carlos J. Segnini R.C
                            Carlos J. Segnini R. @Ekopalypse
                            last edited by

                            @Ekopalypse said in another replacement request for help:

                            \R\h

                            Thanks It works, but I would need to do one by one.
                            Again I would have a list of different names (Aery #B1H, Aery #D2H, etc) so I was looking for another magic script haha

                            EkopalypseE 1 Reply Last reply Reply Quote 0
                            • EkopalypseE
                              Ekopalypse @Carlos J. Segnini R.
                              last edited by

                              @Carlos-J-Segnini-R

                              but then we would need some kind of mapping again,
                              otherwise the script does not know when to use what. :-)

                              1 Reply Last reply Reply Quote 0
                              • Carlos J. Segnini R.C
                                Carlos J. Segnini R.
                                last edited by

                                So this is how the xml file looks
                                e6afaf3a-b1ef-4205-bb9c-515f61d7122f-image.png
                                <Well replace=“false”>
                                <UWI>Alexander Gas Unit 1 #2</UWI>
                                <WellType>Oil</WellType>
                                <ReserveStatusCollection>
                                <Working>
                                <PrimaryProduct>Oil</PrimaryProduct>
                                <WellResCatCollection>

                                and this is how the replacement file would be
                                7c50836c-5aa4-47e1-ac7e-26568264fb30-image.png
                                7dda6e13-179d-4e43-b772-a88ae007c825-image.png
                                Alexander Gas Unit 1 #2 Oil Gas
                                Alexander - Wessendorff 1 A3H Oil Gas
                                Alexander - Wessendorff 1 A4H Oil Gas

                                So the script would look for the first column (Well name), and then replace the word in the second column for the one in the third column:

                                <WellType>Oil</WellType> to <WellType>Gas</WellType>

                                Thanks again, you’re saving my life

                                EkopalypseE 1 Reply Last reply Reply Quote 0
                                • EkopalypseE
                                  Ekopalypse @Carlos J. Segnini R.
                                  last edited by

                                  @Carlos-J-Segnini-R

                                  Is the oil column really needed?
                                  I mean, does it needs to be checked or can the script always look for company and then replace whatever is in WellType with the value from the Gas column?

                                  1 Reply Last reply Reply Quote 1
                                  • Carlos J. Segnini R.C
                                    Carlos J. Segnini R.
                                    last edited by Carlos J. Segnini R.

                                    Edit:
                                    Thinking about, no, no need for that column

                                    EkopalypseE 4 Replies Last reply Reply Quote 0
                                    • EkopalypseE
                                      Ekopalypse @Carlos J. Segnini R.
                                      last edited by

                                      This post is deleted!
                                      1 Reply Last reply Reply Quote 0
                                      • EkopalypseE
                                        Ekopalypse @Carlos J. Segnini R.
                                        last edited by

                                        @Carlos-J-Segnini-R

                                        so you can get rid of the second column and the only thing we need to change is the find expression, I guess.

                                        1 Reply Last reply Reply Quote 1
                                        • EkopalypseE
                                          Ekopalypse @Carlos J. Segnini R.
                                          last edited by

                                          @Carlos-J-Segnini-R

                                          from Npp import editor1, editor2
                                          
                                          replacements = dict(line.split('\t') for line in editor2.getText().splitlines() if line)
                                          
                                          def replace_with(m):
                                              return replacements[m.group(1)]
                                          
                                          # search_for = '(?<=<UWI>).+?(?=</UWI>)'
                                          search_for = '<UWI>(.+)</UWI>\R\h*<WellType>\K.+?(?=</WellType>)'
                                          editor1.rereplace(search_for, replace_with)
                                          
                                          1 Reply Last reply Reply Quote 2
                                          • EkopalypseE
                                            Ekopalypse @Carlos J. Segnini R.
                                            last edited by

                                            @Carlos-J-Segnini-R

                                            btw. if you are interested how these regex search work see here for a pretty good description.

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