How to put symbols after every letter
-
Hello,guys! Pls help to solve my issue in notepad++) I have a text:
1&K@M@Z# no parts
2&iv3c0# wheels,glass
3&KIA# no partsI need:
1.(with out last DOT)1&K.@.M.@.Z# no parts
2&i.v.3.c.0# wheels,glass
3&K.I.A# no parts2.(with last DOT)
1&K.@.M.@.Z.# no parts
2&i.v.3.c.0.# wheels,glass
3&K.I.A.# no partsI want to add symbol “.” after every letter between symbols “&” and “#” with out touching other text.Please help with 2 possible solutions.
Thanks a lot for help! -
@rahim-greed Hello. I’m probably going to leave this for one of the eggheads who know techniques that can achieve this in one pass.
However, if I were to try solving this (in multiple passes) I’d want to know the following about the original text:
-
min and max number of chars between “&” and “#”
-
if it’s guaranteed that there is never “.” between “&” and “#” (which would result in output with triples or greater of consecutive “.”)?
-
-
Your question boils down to “how to do a regex search and replace, but only between some START and END condition”. So, this post by @guy038 shows how to do just that, with a generic regex.
We just then have to figure out the terms to put in. And except for the fact that you wanted every character between those markers to get replaced with itself and a dot, it would have been a fairly simple application of that; I had to do a little bit of debug to get it to work with the every-character-between – but most regex do take some debug.
In your case, the BEGIN (BSR) would have to be
\&
(to signify the literal&
character, because&
has special meaning to regex), and END (ESR) would be#
. The FIND (FR) would be.
(for match any character) and the REPLACE (RR) would be${0}.
(to add the dot after the found character).But actually, because the FR is
.
, that countermands the(?!#)
, so we also need a(?!#)
in the FR to prevent it from going beyond.So
- FIND =
(?-i:\&|(?!\A)\G)(?s:(?!#).)*?\K(?-i:(?!#).)
- REPLACE =
${0}.
will result in
1&K.@.M.@.Z.# no parts 2&i.v.3.c.0.# wheels,glass 3&K.I.A.# no parts
That was the “with last DOT” condition.
Now for the “without last DOT” condition: To tweak it to not allow the last character before the # to match and get a dot appended, add another negative lookahead after the dot in the FR:
- FIND =
(?-i:\&|(?!\A)\G)(?s:(?!#).)*?\K(?-i:(?!#).(?!#))
- REPLACE =
${0}.
will result in
1&K.@.M.@.Z# no parts 2&i.v.3.c.0# wheels,glass 3&K.I.A# no parts
I believe those match your desires.
- FIND =
-
It was pointed out to me that
&
does not have special meaning in the FIND (it just seems to me like it should, because$&
in the replace section does have special meaning). So you can “simplify” by one character in each of the FIND expressions:(?-i:&|(?!\A)\G)(?s:(?!#).)*?\K(?-i:(?!#).)
or(?-i:&|(?!\A)\G)(?s:(?!#).)*?\K(?-i:(?!#).(?!#))
And yes, I did confirm that all 4 of those FIND values (my original two, and the “simplified” two) give the expected values as shown in my previous post.
-
Thank you guys!
-
My humble solutions require you to bang on “replace all” until the status becomes “0 occurrences were replaced…”
Find1: (?<=&)(.*)([^\.])(?!\.)(.*)(?=.#) ==> adds .'s from right, does not add rightmost; tests OK Find2: (?<=&)(.*)([^\.])(?!\.)(.*)(?=#) ==> adds .'s from right, does add rightmost; tests OK ReplWith: $1$2.$3
However, there is also an advantage. With the high powered ones, if you issue a subsequent “replace all”, the data gets mucked up. My regexes do nothing after the job is all done.