Multiple word search and replace within a range start with xxx and end with yyy
-
I have already read from this and know that I can do Multiple word search and replace within a file by
Install Python Script plugin from Plugin Manager.
Create a file with your substitutions (e.g., C:/Temp/Substitutions.txt), separate values with space:good bad great worse fine not
Create a new script:
with open('C:/Temp/Substitutions.txt') as f: for l in f: s = l.split() editor.replace(s[0], s[1]) Run the new script against the text you want to substitute.
However, there are multiple objects in the file, and I just want to change the string within certain objects for example:
object={ ... type=1 b={ abc= 123 def = 456 ghi=789 } } object={ ... type=2 b={ abc= 987 bbb = 654 ccc=321 } } object={ ... type=3 b={ abc= 000 fed = 001 ihg=002 } } object={ ... type=2 b={ abc= 987 bbb = 654 ccc=321 } }
I just want to change every “abc” from every type 2 object.
Is it possible to write a script that the program would start replacing at " type=2" and end at “}” for the whole file? -
Hello, @wagner-leung and All,
I can’t give you advices about a Python script solution but this regex S/R seems to work nice :
- SEARCH
(?s)object((?!object).)+?type=2(.+?)abc=\x20\K987
or
-
SEARCH
(?s)object((?!object).)+?type=2(.+?)abc=\x20\K\d+
if you’re looking for any integer value foreabc
-
REPLACE
Your Integer !
-
Tick the
Wrap around
option -
Select the
Regular expression
search mode -
Click on the
Replace All
button
Best Regards,
guy038
- SEARCH
-
abc is string
@guy038 said in Multiple word search and replace within a range start with xxx and end with yyy:
REPLACE Your Integer !
-
So it is a very common problem here that people don’t provide BEFORE and AFTER data examples.
@guy038 assumed that you were interested in the number after
abc=
when it appears (I think) that you are more interested in theabc
itself. -
Hi, @wagner-leung and All,
Ah OK ! So the variable
abc
contains the string123
. However my reasoning remains identical :I supposed that you want to change the string
987
of the variable abc, when inside an object block, containing the lineType = 2
, by any personal string, inserted in the Replace zone !A better regex, which catches any value of the variable
abc
, would be :SEARCH
(?s)object((?!object).)+?type=2.+?abc=\x20\K.+?$
May be, the best would be to show us a piece of your initial text and your final expected text ! to verify if we’re speaking about the same thing :-)
See you later !
BR
guy038
-
@guy038
I want to change the variable abc but not the string.
As this is another coding language string integer does not make any difference.Here is the intended result
object={ ... type=1 b={ abc= 123 def = 456 ghi=789 } } object={ ... type=2 b={ **aaa**= 987 bbb = 654 ccc=321 } } object={ ... type=3 b={ abc= 000 fed = 001 ihg=002 } } object={ ... type=2 b={ **aaa**= 987 bbb = 654 ccc=321 } }
-
Hi, @wagner-leung, @alan-kilborn and All,
My bad ! So, if I interpret your intended result, strictly, the regex S/R becomes :
SEARCH
(?s)object((?!object).)+?type=2((?!object).)+?\K\babc(?=\h*=)
REPLACE
**aaa**
Hope this version is the good one ! Of course, you may change the line
type=2
and/or the variableabc
as you like !Cheers,
guy038
P.S. :
An other formulation of the search regex, using the free-spacing mode
(?x)
, would be :SEARCH
(?xs) object ((?!object).)+? type=2 ((?!object).)+? \K\b abc (?=\h*=)
For instance, the following search regex would search for the variable
abc
, in anyobject
block containing, either, the linetype=2
ortype=3
SEARCH
(?xs) object ((?!object).)+? type=[23] ((?!object).)+? \K\b abc (?=\h*=)