Regex Find & Replace after Specific Text
-
Basically. I have a bunch of text files that someone incorrectly plugged a value into roughly 650 files. The + was supposed to be a - and vice versa. I am doing my best to come up with a way to mass edit these. There is alot more information in these documents other wise I would just find and replace the +/- as needed.
The closest I got:
/^.*Pattern:.*(?:\+|-)$This is roughly what the files look like.
//extra crap //c:/imessedup/i-don't-know //HHLAU_MD01_HIGHFIVES //*****INFORMATION ABOUT MODULATION*** Pattern: +-+++---++--++--+++--
moderator added code tags to make it readable -
Going to the Generic Regex formula FAQ, and going to the Replacing in a specific zone, I went to the simplified formula for a single-line zone:

I then translated that into a three-regular-expression sequence.
-
Pluses to smilies:
SEARCH(?-s)(?-i:^Pattern\:|(?!\A)\G).*?\K(?-i:\+)
REPLACE☺ -
Minuses to pluses:
SEARCH(?-s)(?-i:^Pattern\:|(?!\A)\G).*?\K(?-i:\-)
REPLACE+ -
Smilies to minuses:
SEARCH(?-s)(?-i:^Pattern\:|(?!\A)\G).*?\K(?-i:☺)
REPLACE-
(Mode = regular expression for all)
This will only do replacements on lines that start with
Pattern:
If you actually have spaces or tabs beforePattern, then change the middle portion to^\h*Pattern\: -
-
@PeterJones First off, thank you for taking the time to respond. I’m looking at this in regex101 and it seems to work there but when I took it to Notepad++(transcribed to stand-alone system) it isn’t working. I’ve quintuple checked character for character. I am using a version of Notepad++ from 2018 though. Also, have tried this with . matches newline and without
-
It worked just fine for me.
But something I didn’t emphasize in my post, even though it was mentioned in the “Replacing in a specific zone” documentation, you must use Replace All: if there is a
\Kin the Find what: regex, you have to use Replace All.The .-matches-newline is irrelevant, because the expression has
(?-s)which overrides that checkbox. -
@PeterJones I’m going to download the newest version and try it again. Also will copy over your text verbatim so I don’t mess anything up.
-
Ah, yes, v7.9.1 fixed a bug in
\A, which may have been making the regex not work right. I tried in v7.7.1, and it only replaced the first plus with the smiley, whereas in v7.9.5 (soon after the fix), it worked as I originally described.This requirement was actually noted in the IMPORTANT notes in the “Replacing in a region” FAQ
-
@PeterJones Ok. I messed up when I told you Pattern:. It was actually all caps. Now that is corrected but when I click replace all it is only going one character at a time.
-
@Chase-Rankin said in Regex Find & Replace after Specific Text:
Now that is corrected but when I click replace all it is only going one character at a time.
That happens if you are on a version before v7.9.1, because of the bug in
\A. For this expression to work, you need to be on v7.9.1 or newer. -
@PeterJones Thanks so much for all the help. I got it done with the old version by just force clicking through it all. Pressing my IT dept to install the newest version. Thanks again!
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