• Login
Community
  • Login

*Help* How to change multiple unknown text string with just one?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
27 Posts 6 Posters 3.1k 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.
  • G
    guy038
    last edited by guy038 Jun 14, 2020, 9:36 PM Jun 14, 2020, 11:00 AM

    Hi, @chuny_ok, @gurikbal-singh and All,

    Of course, your S/R, @gurikbal-singh, is more simple and obvious but could grasp too many occurrences, as it does not care about the tags and the attributes involved, with the same value :-((

    But I admit, that in case of few found occurrences, the use of the step by step replacement, with the Replace button, after verification, may be enough for @chuny_ok !

    Best Regards

    guy038

    1 Reply Last reply Reply Quote 2
    • C
      Chuny_OK
      last edited by Jun 14, 2020, 5:19 PM

      Thank you @guy038 and @gurikbal-singh
      I tried with your suggestions and all of them worked.
      Much appreciated!
      Thanks again!

      1 Reply Last reply Reply Quote 2
      • C
        Chuny_OK
        last edited by Jun 22, 2020, 10:15 PM

        @guy038 Hello my friend, I have another one for you!
        Now, I’m not sure if this is possible at all but, let’s try.

        I have several blocks within an XML file
        They are a little something like this

        <Instance>
        <property name=“Evaluator” type=“int” value=“4”/>
        <property name=“zoomValue” type=“float” value=“0.8”/>
        <property name=“Framingn” type=“int” value=“2”/>
        <property name=“Name” type=“string” value=“High”/>
        <Property>
        <property name=“PropertyName” type=“string” value=“Whatever”/>
        </Property>
        </Instance>

        Like I said, there are many blocks like that in different places of the same XML but with different values, different properties, etc.
        But they are all within the <Instance> </Instance> tags.

        I’d like to replace all of them, not matter the content inside those tags with only one <Instance> </Instance> of my own.
        Obviously, doing that one by one would take about two hours since there are probably between 70 and 100 I’d need to change. So is there any way to do what I’m after?.

        Thank you very much in advance.

        A 1 Reply Last reply Jun 22, 2020, 10:59 PM Reply Quote 0
        • A
          astrosofista @Chuny_OK
          last edited by Jun 22, 2020, 10:59 PM

          Hi @Chuny_OK

          -Try this:

          Open the Replace dialog (Ctrl + H) and type in:

          Search: (?s)(<Instance>).*?(</Instance>)
          Replace: $1HELLO$2

          Replace "HELLO" with your string.
          Check the Wrap around option
          Select the Regular expression search mode
          Click on the Replace All button

          C 1 Reply Last reply Jun 22, 2020, 11:08 PM Reply Quote 1
          • C
            Chuny_OK @astrosofista
            last edited by Jun 22, 2020, 11:08 PM

            @astrosofista said in *Help* How to change multiple unknown text string with just one?:

            (?s)(<Instance>).*?(</Instance>)

            Thank you! It worked like a charm!
            Thank you for your quick reply too!

            A 1 Reply Last reply Jun 23, 2020, 3:42 AM Reply Quote 0
            • A
              astrosofista @Chuny_OK
              last edited by Jun 23, 2020, 3:42 AM

              @Chuny_OK

              You’re welcome. If you want to get involved in regex, let me suggest you to take a look at this page:

              https://npp-user-manual.org/docs/searching/#regular-expressions

              Have fun!

              G 1 Reply Last reply Jul 4, 2020, 2:05 PM Reply Quote 2
              • G
                George Fisher @astrosofista
                last edited by Jul 4, 2020, 2:05 PM

                Instead of starting a new thread I searched and found this one which appears to address my problem.

                I have several files that contain this line, price: 12000 where the number varies in every file… I tried with the (*) filter to change that to price: 1000 in all of the files at once but the program wouldn’t cooperate. I’m not a coder so some of the above examples are confusing to me. Can someone suggest what to enter in the replace with: line?
                Thanks. ;)

                1 Reply Last reply Reply Quote 0
                • G
                  guy038
                  last edited by guy038 Jul 4, 2020, 2:40 PM Jul 4, 2020, 2:38 PM

                  Hi @george-fisher and All,

                  Easy with regular expressions !

                  • Open the Find in Files dialog ( Ctrl + Shift + F )

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

                  • REPLACE price:\x201000

                  • Fill in the Filters: and Directory: fields, as desired

                  • Select the Regular expression search mode

                  • Click on the Replace in Files button

                  • Click on the OK button of the confirmation dialog

                  Voila ;-))


                  Notes :

                  • First, the (?-i) ensures that the search process is performed in a non-insensitive way

                  • Then, the part price: looks for this literal string, with this exact case

                  • Now, \x20 is the hexadecimal syntax of a space character

                  • Finally the \d+ syntax represents any non-null consecutive range of digits, so any decimal number

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 0
                  • C
                    Chuny_OK
                    last edited by Chuny_OK Sep 19, 2020, 7:49 PM Sep 19, 2020, 7:48 PM

                    Hello again!

                    I need some more help.
                    I have several lines within an XML file that have something like this:

                    <property name=“dateTimeHour” type=“uint32” container=“StaticArray” arraySize=“1”>
                    <value>12</value>

                    In which only the numerical value that’s within the <value>X</value> is different in each line along different parts of the same XML File.
                    I would like to change all those different values with the same one, just one.

                    Could any of you kind human beings help me?.

                    Thank you!

                    A 1 Reply Last reply Sep 19, 2020, 8:48 PM Reply Quote 0
                    • A
                      astrosofista @Chuny_OK
                      last edited by Sep 19, 2020, 8:48 PM

                      Hi @Chuny_OK

                      -Try the following:

                      Open the Replace dialog (Ctrl + H) and type in:

                      Search: (?-s)(?<=<value>)(\d)+(?=</value>)
                      Replace: 000

                      Replace 000 with your replacing number.
                      Check the Wrap around option
                      Select the Regular expression search mode
                      Click on the Replace All button

                      Have fun!

                      C 1 Reply Last reply Sep 25, 2020, 2:38 AM Reply Quote 3
                      • C
                        Chuny_OK @astrosofista
                        last edited by Sep 25, 2020, 2:38 AM

                        @astrosofista

                        Thank you!
                        Although, there are many other strings with
                        <value>X</value> but with different properties.

                        Like:
                        <property name=“dateTimeHour” type=“uint32” container=“StaticArray” arraySize=“1”>
                        <value>12</value>
                        <property name=“dateTimeMinute” type=“uint32” container=“StaticArray” arraySize=“1”>
                        <value>12</value>
                        <property name=“overrideAtmosphere” type=“bool” container=“StaticArray” arraySize=“1”>
                        <value>true</value>

                        I only need to change those with DateTimeHour.
                        If I do what you suggested, every number in-between the <value> </value> tags will get changed!

                        1 Reply Last reply Reply Quote 0
                        • Terry RT
                          Terry R
                          last edited by Terry R Sep 25, 2020, 2:52 AM Sep 25, 2020, 2:51 AM

                          @Chuny_OK said in *Help* How to change multiple unknown text string with just one?:

                          I only need to change those with DateTimeHour

                          How about using the following regex in place of the current one:
                          Search:(?-s).+?dateTimeHour.+\R<value>\K(\d)+(?=</value>)

                          So it finds the line with dateTimeHour, then progresses to the NEXT line, finds the number and what ever you type into the Replace field is what is inserted.

                          As this uses the \K regex command you must use the “Replace All” button. So if you need to cyle through the changes one at a time using the singular “Replace” button please come back with that request and another regex could be made to suit.

                          Terry

                          C 2 Replies Last reply Sep 25, 2020, 2:52 AM Reply Quote 1
                          • C
                            Chuny_OK @Terry R
                            last edited by Sep 25, 2020, 2:52 AM

                            @Terry-R

                            That worked like a charm, Terry!
                            Thanks a lot!

                            1 Reply Last reply Reply Quote 1
                            • C
                              Chuny_OK @Terry R
                              last edited by Chuny_OK Sep 26, 2020, 9:41 PM Sep 26, 2020, 9:39 PM

                              @Terry-R

                              Hello Terry
                              Unfortunately, after further analysis, somehow when I use that expression, the file gets messed up after some of those lines I mentioned above.

                              What I was doing is, replacing all those different dateTimeHour values, with the number 17.

                              In one particular line, (it’s like after the 8th modification), I get this:

                              <property name=“dateTimeHour” type=“uint32” container=“StaticArray” arraySize=“1”>
                              <value>1717

                              And then the file ends.

                              But originally, the file continued with several other strings.
                              In the original file, that line was like this:

                              <property name=“dateTimeHour” type=“uint32” container=“StaticArray” arraySize=“1”>
                              <value>19</value>

                              And, like I said, there were plenty other lines after that. The document was huge.
                              But after using your command, I get what I described above and then the file ends.

                              I tried placing it with other values, like 0. But no matter which value I enter, that value gets duplicated
                              For example>
                              <property name=“dateTimeHour” type=“uint32” container=“StaticArray” arraySize=“1”>
                              <value>00

                              …And then, the file ends.

                              Very strange.

                              1 Reply Last reply Reply Quote 0
                              • Terry RT
                                Terry R
                                last edited by Sep 26, 2020, 10:17 PM

                                @Chuny_OK said in *Help* How to change multiple unknown text string with just one?:

                                And then the file ends.

                                I presume you mean the file gets truncated? In large files with some types of look-aheads there can be instances of this occuring, but I wouldn’t have thought this in your case.
                                Try this alternative regex.
                                Find What:(?-s)(.+?dateTimeHour.+\R<value>)\d+
                                Replace With:\1xx

                                Where xx is the number you wish to use, it can be any length i.e. 1, 3765 etc. In this case as we are capturing the previous line and a bit of the current line (so we only get the datetimehour values you want to change) we need to write it back, hence the \1 in front of the number you want to insert.

                                Terry

                                C 1 Reply Last reply Oct 3, 2020, 5:57 AM Reply Quote 2
                                • C
                                  Chuny_OK @Terry R
                                  last edited by Chuny_OK Oct 3, 2020, 5:58 AM Oct 3, 2020, 5:57 AM

                                  @Terry-R
                                  Hello Terry
                                  Doesn’t quite work.

                                  If I do that, the whole document gets replaced by the number I entered :(

                                  The strange thing is that when I first started this thread, the help I was getting was always working! And the files are still XML, the structure is quite similar, although the properties are different. So I don’t get why neither the first tips I was provided nor the new ones are working.

                                  C Terry RT 2 Replies Last reply Oct 3, 2020, 6:00 AM Reply Quote 0
                                  • C
                                    Chuny_OK @Chuny_OK
                                    last edited by Oct 3, 2020, 6:00 AM

                                    @Chuny_OK

                                    For instance, there’s another thing I’d like to change:

                                    <property name=“gameMinExposure” type=“float” container=“StaticArray” arraySize=“1”>
                                    <value>-14</value>

                                    This line appear multiple times along this large document, but with different values along the way.
                                    If I wanted to replace al those different values with just one (like -10 for example), I can’t do that.

                                    1 Reply Last reply Reply Quote 0
                                    • Terry RT
                                      Terry R @Chuny_OK
                                      last edited by Oct 3, 2020, 7:20 AM

                                      @Chuny_OK said in *Help* How to change multiple unknown text string with just one?:

                                      the whole document gets replaced by the number I entered :(

                                      My regex works by capturing part of 1 line, then the first part of the next line. If the “whole” document is being replaced it suggests you don’t have many or most likely ANY CRLF (carriage return line feeds in it.

                                      When the file is loaded in Notepad++ by default the left side shows line numbers. Can you determine if it only contains 1 or a small number of lines? If so then I would need to do some more testing, but my initially reaction would be to change that regex to:
                                      Find What:(?-s)(.+?dateTimeHour.+\R?<value>)\d+
                                      This will not force a CRLF between the 2 lines, but if it exists then it will include it.

                                      Terry

                                      1 Reply Last reply Reply Quote 2
                                      • Terry RT
                                        Terry R
                                        last edited by Oct 3, 2020, 7:31 AM

                                        Actually thinking further another amendment is needed so try
                                        Find What:(?-s)(.+?dateTimeHour.+?\R?<value>)\d+
                                        The .+? minimally tries to capture up to a possible CRLF sequence, and just beyond to the first (hopefully) sequence of the <value> tag.

                                        Terry

                                        C 1 Reply Last reply Oct 5, 2020, 12:47 AM Reply Quote 2
                                        • C
                                          Chuny_OK @Terry R
                                          last edited by Oct 5, 2020, 12:47 AM

                                          @Terry-R said in *Help* How to change multiple unknown text string with just one?:

                                          (?-s)(.+?dateTimeHour.+?\R?<value>)\d+

                                          Look mate,
                                          I uploaded the file here: https://www83.zippyshare.com/v/rHBggLMf/file.html

                                          Please look for lines like:

                                          **<property name=“dateTimeHour” type=“uint32” container=“StaticArray” arraySize=“1”>

                                          And/Or

                                          **<property name=“gameMinExposure” type=“float” container=“StaticArray” arraySize=“1”>

                                          Where the numbers between the <value> </value> tags can be different everytime you search for them.

                                          And see if it works for you.

                                          Thank you again.

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