Help with extended search/replace
-
Hi, suppose that I have this file:
dada test1
daeq test2
dawf test3
fawq test4
aafa test5
faag test6I would get this with replace:
dada test1
:example1
daeq test2
:example2
dawf test3
:example3
fawq test4
:example4
aafa test5
:example5
faag test6
:example6How can i had :example(number) between all lines?
ThankYou,
Matteo. -
3 steps
First step:
place the cursor onto the first line first position (or press ctrl+home)
press alt+c (the column mode editor opens)
select Number to Insert
Initial number = 1
Increase by = 1
Repeat = 1
press OKShould result in
1dada test1
2daeq test2
3dawf test3
4fawq test4
5aafa test5
6faag test6Step2
press alt+c again (cursor should still be at line 1 position 1)
select text to insert and type :exampleShould result in
:example1dada test1
:example2daeq test2
:example3dawf test3
:example4fawq test4
:example5aafa test5
:example6faag test6Step3 (cursor should still be at line 1 position 1
press ctrl+h
uncheck wrap around
check regular expression
find what should contain(:example\d+)(.+)
replace with
\2\r\n\1
press replace all
should result in
dada test1
:example1
daeq test2
:example2
dawf test3
:example3
fawq test4
:example4
aafa test5
:example5
faag test6
:example6Regular expression for find what: (:example\d+)(.+)
() means we do have two capture groups \1 and \2
\1 contains :example followed by one or more digits
and
\2 contains the rest including end of line charsIn replace with we reorganize the string as \2\r\n\1 means
print the second matching group=\2
add an end of line char=\r\n
print the first matching group = \1Sounds more complicated than actually is.
Cheers
Claudia -
Hello, @matteo-colombo and @claudia-frank,
Hi, Claudia ! Of course, your solution works fine, but you certainly noticed, that any number, ending each line, wanted by Matteo, is present twice, on two consecutive lines, didn’t you ? Just a nice case for regexes, only !!
So,… what about this simple regex S/R :
SEARCH
(?-s)^.+?(\d+\R)
REPLACE
$0:example\1
Preferably :
-
Move back to the very beginning of your file
-
In the Replace dialog, as Claudia said, uncheck the Wrap around option et check the Regular expression option
-
Fill up the search and replace zones
-
Click on the Replace All button
Et voilà !
Notes :
-
As usual, the
(?-s)
modifier implies that any dot special character which match a single standard character, only -
The ending part
(\d+\R)
represents any number, whatever the number of digits, followed by the End of Line characters of the current line. As this part is surrounded by parentheses, it is stored as group 1 -
The middle part
^.+?
stands for any non-null text, from beginning of line till the last character, before the ending number -
In replacement, we rewrite, first, the entire searched line (
$0
), followed by the string :example, followed itself, by the group 1 (\1
) that is to say the ending number, followed by the End of Line characters of the current line !
Best Regards,
guy038
-
-
Hi Guy,
to be honest, I didn’t think about it but you are right, if this,
I mean that the number at the end of the line is the number
which should be used in the “example” line,
is the case, then your solution is much nicer than mine :-)Cheers
Claudia -