How to replace a specific text in lines that contains a specific word.
-
I have a config file, that i want to manipulate with notepad++.
It contains lines like this:railcraft {
B:dustCharcoal_false=false
B:dustObsidian_false=false
B:dustSaltpeter_false=false
B:dustSulfur_false=false
B:ingotCopper_false=false
B:ingotLead_false=false
B:ingotSteel_false=false
B:ingotTin_false=false
B:nuggetCopper_false=false
B:nuggetIron_false=false
B:nuggetLead_false=false
B:nuggetSteel_false=false
B:nuggetTin_false=false
B:plateCopper_true=true
B:plateIron_true=true
B:plateSteel_true=true
B:plateTinAlloy_true=true
}What i want to do, is to replace all the “=false” texts to “=true” in lines, that contains the word “ingot”.
For example this block of text after the replace would look like this:
railcraft {
B:dustCharcoal_false=false
B:dustObsidian_false=false
B:dustSaltpeter_false=false
B:dustSulfur_false=false
B:ingotCopper_false=true
B:ingotLead_false=true
B:ingotSteel_false=true
B:ingotTin_false=true
B:nuggetCopper_false=false
B:nuggetIron_false=false
B:nuggetLead_false=false
B:nuggetSteel_false=false
B:nuggetTin_false=false
B:plateCopper_true=true
B:plateIron_true=true
B:plateSteel_true=true
B:plateTinAlloy_true=true
}I started to learn about the search mechanism of notepad++, but it’s way too complex for me to figure it out. (i spent an hour learning it so far)
Can you give me the syntax for this specific use case, and explain it too?Thanks in advance!
-
You might try:
find:
(?-is)ingot.*?=\Kfalse
repl:true
mode: regular expression -
@alan-kilborn Thanks! It was a crazy fast reply!
It did work almost perfectly. What i discovered, is that there are some lines, that contains “Ingot” (capital i) for some reason, and i would like to change them too. Can you modify your search so it will be independent from that factor? (of Course i can make two replaces with your syntax, i am just curious, what would be the solution for that)
-
This post is deleted! -
@zsombor-szarka said in How to replace a specific text in lines that contains a specific word.:
some lines, that contains “Ingot” (capital i) for some reason, and i would like to change them too.
Change the
(?-is)
part to(?i-s)
.OR
Make that part
(?-s)
and then use theMatch case
checkbox to do your will.It did work almost perfectly.
No it worked perfectly, not almost.
Just because you left something out of your problem statement doesn’t mean the solution provided was deficient. :-) -
Hello @zsombor-szarka, @alan-kilborn, @peterjones and All,
I may be wrong but I think that the correct search expression is rather :
SEARCH
(?i-s):ingot.*\Kfalse$
Indeed, the part
_false
, before the=
sign, must not be changed !Best Regards,
guy038
-
@guy038 said in How to replace a specific text in lines that contains a specific word.:
Indeed, the part _false, before the = sign, must not be changed !
That’s why Alan’s expression has the
.*?=
before the\K
, to make sure everything before and including the equals is consumed without change -
@guy038 said in How to replace a specific text in lines that contains a specific word.:
I may be wrong but I think that the correct search expression
I think the original is solution is fine.
Perhaps the?=
in it is confusing (looks maybe like a(?=
…)
construct?)
But really it just forces=false
to be matched. -
Hi, @zsombor-szarka, @alan-kilborn, @peterjones and All,
My bad ! Alan, I didn’t notice the equal sign in your version :-(
BR
guy038
-
Not a problem, Guy.
Like I said maybe I shouldn’t have done it the way I did, because it really does look like that other construct.BTW, good to see you posting.
You haven’t been very active lately.
Hopefully you are “back” and will be posting on a more regular basis now. -
No it worked perfectly, not almost.
Just because you left something out of your problem statement doesn’t mean the solution provided was deficient. :-)Well i can’t argue with that, i should have formulated my sentences better.
Anyway now it works as i wanted to, so thanks a lot!Have a wonderful day!
-
I started to learn about the search mechanism of notepad++, but it’s way too complex for me to figure it out. (i spent an hour learning it so far)
Well, any time invested in this endeavor is time well spent (for the NEXT time).
Can you give … explain it too?
I basically gave the syntax:
(?-s)ingot.*?=\Kfalse
It means:
(?-s)
: don’t let a following.
match a line-ending
ingot
: match the literal text
.*?
: match any characters zero or more times, minimally
=
: match literal equal
\K
: forget the match to this point (we don’t want to changeingot
followed by whatever followed by=
)
false
: match the literal textWe replace with only
true
because the matched text will befalse
at this point. -
@alan-kilborn
Thanks a lot (again)!
I understand everything except this line:(?-s)
: don’t let a following.
match a line-endingI can’t figure out what do you mean by that… Can you elaborate?
-
@zsombor-szarka said in How to replace a specific text in lines that contains a specific word.:
I understand everything except this
You even understand the
\K
fully? :-)Ok so the
(?-s)
is equivalent to leaving this checkbox unchecked:But what it means is that using a
.
further along in the expression will NOT match a line-ending character.When you originally said:
is to replace all the “=false” texts to “=true” in lines
The “in lines” part is a real key that
(?-s)
should be used.Otherwise, you could get a match like this (definitely unwanted):
Note that I changed your data a bit in the above.
See how there is a match on line 10 even though there is no
ingot
on line 10? The ingot part starts on line 9… :Note that what I’m showing now is if you use
(?s)
instead of(?-s)
. The-
turns off the ability to match line ending characters.So the
(?-s)
is a safety-valve on the expression, in this instance saying “everything must be on the same line”.Hopefully that makes a bit of sense.
-
@alan-kilborn said in How to replace a specific text in lines that contains a specific word.:
So the (?-s) is a safety-valve on the expression, in this instance saying “everything must be on the same line”.
This sentence sums it up clearly, after this one i was sure i understood everything what you just wrote.
I don’t know why did you ask whether i understood the \k fully, but i did.
In fact everything was simple, what probably confused me, is that i did not catch what did you mean by “following .” i had no idea that by the dot you meant the whole search.Now everything is clear and i even have a greater understanding on some of the things, i read on the notepad++ wiki.
Thanks!BS
-
You said :
You haven’t been very active lately.
You’re quite right ! Well, there’s a simple explanation to this :
-
First of all, at this time of the year, I am rather involved in ski, which is a passion of mine !
-
Secondly, each Wednesday, with eight other people, we supervise , at the
Chamrousse
station,( the nearest from Grenoble )50
children about, from 5 to 14 years old, in ski lessons, for any level, fromDébutants
toFlèche de Bronze
( approximatively, from american ski level1
to8
: refer, for instance, https://www.tripsavvy.com/skiing-ability-levels-3007976 ) -
And thirdly, last week-end, we were 13 people at Courchevel, a very well-known French station, with a wonderful sunny and blue weather ! Really, we had lunch, Sunday midday, on the terrace of the hotel and it was rather hot, at about
1700 m
!
Next Thursday, we’ll be, for the day, at the Karellis station :
Best regards,
guy038
-
-
@guy038 said in How to replace a specific text in lines that contains a specific word.:
First of all, at this time of the year, I am rather involved in ski, which is a passion of mine !
I’m not an excellent skier, but nothing sounds better to me right now than joining you on those slopes! :-)
-