Find and Replace between {}
-
Hey Guys,
do anyone of you got an Idea…
I need to replace every “.” to " \ " if its in {}… for this example:
This is a example.text:text.text
More{example.text}After all
This is a example.text:text.text
More{example\text}
These are tested by myself:
Thanks in advance!
-
In the future, please use
```
```
to enclose blocks of example text, rather than using an image. That way other users can copy your example text and paste it into Notepad++.In any case, a reasonable way to replace
.
with\
if it is inside{}
is to open the find/replace form (Ctrl+H with default keybindings) and use the following fields:
Find what:(?s)(?:\{|(?!\A)\G)[^{}]*?\K(?:[^}]*(?:\Z|(?=\{))(*SKIP)(*FAIL)|\.)
Replace with:\
Search Mode:Regular expression
You didn’t specify whether it was acceptable for there to be any
{
in between{}
, so I assumed that no{
or}
could be between the{}
. You also didn’t specify whether the opening{
and the closing}
could be on separate lines. If you want to tweak the regex because I was wrong about your requirements, you are free to do that, but I won’t do it for you because making those changes could be a good learning experience for you.
This regular expression makes use of the
SKIP
andFAIL
backtracking control verbs and the generic regex for searching in a region of text. Most of the complexity comes from the need to ignore.
characters following a{
that does not have a matching}
.
This regex-replace converts
{ foo.txt { {foo.txt} {foo.bar.baz.txt rejkrje.kj rjrj.rj} {foo. bar.] } foo . . . . bar ]{ . . . . { . .
to
{ foo.txt { {foo\txt} {foo\bar\baz\txt rejkrje\kj rjrj\rj} {foo\ bar\] } foo . . . . bar ]{ . . . . { . .
-
Hey @Mark-Olson !
First, I would like to thank you very much! You have helped me a lot, and you are absolutely right. Next time, I will describe my specifications more precisely and not include images!
I think this topic can be closed.
Thank you very much! -
@masterofdisaster said in Find and Replace between {}:
I think this topic can be closed.
I’m glad I could help! “Closing” topics is not a thing here though.
In any case, I’d like to revise my regex-replace a bit, and change it to:
Find what:(?:\{(?=[^{}]*\})|(?!\A)\G)[^{}]*?\K\.
Replace with:\
Search Mode:Regular expression
The only difference from my previous recommendation is the
Find what
field. I was rushing when I came up with my previousFind what
regex, and I missed a scenario where it could exhibit catastrophically bad performance, namely the case of{
followed by very many (say, 2000 or more).
characters, followed by}
, which my old regex-replace would match correctly, but taking time proportional to the square of the length of the document.This issue illustrates the importance of thinking carefully when making regular expressions, especially ones using lookahead and lookbehind; it is very easy to open yourself up to catastrophic backtracking. Of course, it also illustrates the power of those more advanced regex capabilities; the much weaker engine of (for example) VSCode could not do this regex-replace.
-
Hello, @masterofdisaster, @mark-olson and All,
Hi, Mark, could you verify that the regex S/R, below, seems to be the minimum one to get the expected results ?
I personally tested it with about
12,100
characters between a{
and a}
boundaries, and with or withoutdot
characters inside this range !SEARCH
(?:\{|(?!\A)\G)[^}]*?\K\.
REPLACE
\
Best Regards,
guy038
-
@guy038
Without even testing it, I agree that(?:\{|(?!\A)\G)[^}]*?\K\.
will not have the scaling issues, but it does not correctly handle the edge case of{.
or any other document with{
followed by.
without the matching}
afterwards.