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



  • Hello everybody!
    I hope you are all alright and healthy.

    So, I’ve searched for this on Google and on this Forum and haven’t been able to find it. Either that, or the solutions I found didn’t work for me (probably because I’m just too dumb).

    So, I have a big XML file in which I have multiple strings with a constant text, followed by different strings.
    I’d like to keep that constant text and change all of the different options that follow, with another new constant text.

    For example -
    Example 1: <property name=“Name” type=“string” value=“Iso”/>
    Example 2: <property name=“Name” type=“string” value=“Bin”/>
    Example 3: <property name=“Name” type=“string” value=“Wire”/>
    Example 4: <property name=“Name” type=“string” value=“Whatever”/>

    And I’d like to change the Iso, Bin, Wire and Whatever for: HELLO.
    Obviously, all of those lines are in different places of the file, and there are many more. I just simplified the task at hand for the sake of simplicity.

    I hope you can help a newbie out.

    Thank you all in advance.



  • Hello, @chuny_ok and All,

    No problem, with regular expressions ! So :

    • Open the Replace dialog ( Ctrl + H )

    • SEARCH (?-s)<property name="Name" type="string" value="\K.+(?="/>)

    • REPLACE HELLO

    or

    • SEARCH (?-s)(<property name="Name" type="string" value=").+

    • REPLACE \1HELLO"/>

    • Tick, preferably, the Wrap around option

    • Select the Regular expression search mode

    • Click on the Replace All button ( Do not use the step by step Replace button, with the first S/R ! )

    If OK, I could develop how these regexes work, next time ;-))

    Best Regards,

    guy038



  • or for custom replace another idea is

    find what:Iso|Bin|Wire|Whatever
    replace with:HELLO
    Select the Regular expression mode



  • 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



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



  • @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.



  • 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



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



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



  • 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. ;)



  • 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



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



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


Log in to reply