Find and Replace
-
Hello,
I have an xml file.
If I want to replace for example, for all the groups are the character string “TOTO”, the state “OLD” by the state “NEW”, and keeping the character string which contains “TOTO”:
Example :
I want this block:
<Key>String</Key>
<Value ProtectInMemory=“True”>aferTOTObzd1</Value>
<Key>Status</Key>
<Value>OLD</Value>becomes:
<Key>String</Key>
<Value ProtectInMemory=“True”>aferTOTObzd1</Value>
<Key>Status</Key>
<Value>NEW</Value>how to do ?
To search, I search in extended mode / regular expression :
<Key>String</Key>\r\n<Value ProtectInMemory=“True”>.*TOTO.*</Value>\r\n<Key>State</Key>\r\n<Value>OLD</Value>
But what should be replaced?
Thanks.
My text in french / Mon texte en français :
Bonjour,
J’ai un fichier xml.
Si je veux remplacer par exemple, pour tous les groupes sont la chaîne de caractère contient “TOTO”, l’état “OLD” par l’état “NEW”, et en conservant la chaîne de caractères qui contient “TOTO” :
Exemple :
je veux que ce bloc :
<Key>String</Key>
<Value ProtectInMemory=“True”>aferTOTObzd1</Value>
<Key>State</Key>
<Value>OLD</Value>devienne :
<Key>String</Key>
<Value ProtectInMemory=“True”>aferTOTObzd1</Value>
<Key>State</Key>
<Value>NEW</Value>comment faire ?
Pour rechercher, je cherche en mode étendu / expression régulière :
<Key>String</Key>\r\n<Value ProtectInMemory=“True”>.*TOTO.*</Value>\r\n<Key>State</Key>\r\n<Value>OLD</Value>
Mais il faut mettre quoi pour remplacer ?
Merci.
-
The forum messed up your expression (because you didn’t post it correctly; see HERE ), but, in general you want to use regular expression mode and then you can do something like:
Find:
(foo)textToChange(bar)
Repl:${1}newText${2}
Search mode: Regular expressionAlternately:
Find:
(foo)textToChange(?=bar)
Repl:${1}newText
Or, maybe you don’t even need what comes after your “textToChange” (because it is constant, you know what it is, and it isn’t going to change), in which case:
Find:
(foo)textToChange
Repl:${1}newText
-
Thank you very much @Alan-Kilborn for your quick response, it’s very nice!
I struggled to understand but I succeeded:
In the end, for my example, we must replace in regular expression:
(<Key>String</Key>\r\n<Value ProtectInMemory=“True”>.*TOTO.*</Value>\r\n<Key>State</Key>\r\n<Value>)OLD(</Value>)
By :
${1}NEW${2}
And it works!
Thanks again.
Regarding my incorrect post, I didn’t understand what is wrong in my post (sorry, I’m not very good in English). Do I need to change something or is it ok?
Good day.
My text in french / Mon texte en français :
Merci beaucoup Alan pour ta réponse rapide, c’est très sympa !
J’ai peiné à bien comprendre mais j’ai réussi :
Au final, pour mon exemple, on doit remplacer en expression régulière :
(<Key>String</Key>\r\n<Value ProtectInMemory=“True”>.TOTO.</Value>\r\n<Key>State</Key>\r\n<Value>)OLD(</Value>)
Par :
${1}NEW${2}
Et cela fonctionne !
Merci encore.
Concernant mon post incorrect, je n’ai pas compris ce qui ne convient pas dans mon message (désolé, je ne suis pas très bon en anglais). Dois-je modifier quelque chose ou ce n’est pas gênant ?
Bonne journée.
-
@toto753 said in Find and Replace:
Regarding my incorrect post, I didn’t understand what is wrong in my post (sorry, I’m not very good in English). Do I need to change something or is it ok?
It is ok NOW because you edited it.
If you would have followed proper procedure from the start, no edits would have been necessary, and I wouldn’t have had to guess at what your expression was. -
@toto753 said in Find and Replace:
I struggled to understand but I succeeded
… And it works!Good job!
-
@Alan-Kilborn said in Find and Replace:
Find:
(foo)textToChange(bar)
Repl:${1}newText${2}
Search mode: Regular expressionI’m curious as to why the
${1}
and${2}
style syntax here is suggested rather than\1
and\2
? I myself have only use the${1}
style when dealing with more than 9 back references. -
@mkupper said in Find and Replace:
I’m curious as to why the
${1}
and${2}
style syntax here is suggested rather than\1
and\2
? I myself have only use the${1}
style when dealing with more than 9 back references.I don’t speak for Alan, but I personally use that syntax:
-
It’s more extensible for the end user: if we train newbies to use
\1
and\2
, then they will think that they’ve “learned” the syntax (without ever looking it up), then when they get to their first substitution with 10 or more groups, they will be surprised when they fail. If, OTOH, we use examples in the forum that always use the${1}
syntax, it is 100% extensible no matter how many groups they try to use in later expressions. -
It provides a visual clue when there are no spaces between:
\1newText\2
“runs together” more in my mind than${1}newText${2}
, so it’s easier for me to mentally parse -
Since I learned regular expressions in Perl, and Perl has long recommended using
${1}
(or at least$1
) syntax and warned against\1
-style replacements, I got in the habit of not using\1
-style in the replacements (even though the reason for the warning in Perl is irrelevant to Boost)
-
-
Thank you all for your responses.
I also find it more logical, with my experience on computer technologies, to use ${1} rather than \1.