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 notCreate 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 aroundoption -
Select the
Regular expressionsearch mode -
Click on the
Replace Allbutton
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 theabcitself. -
Hi, @wagner-leung and All,
Ah OK ! So the variable
abccontains the string123. However my reasoning remains identical :I supposed that you want to change the string
987of 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=2and/or the variableabcas 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 anyobjectblock containing, either, the linetype=2ortype=3SEARCH
(?xs) object ((?!object).)+? type=[23] ((?!object).)+? \K\b abc (?=\h*=)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login