help! regular express
-
Hello, I want to add brackets around multi-line text
before
some text
some text
some text
…after
{
some text
some text
some text
…
} -
Hello, @李唯任 and All,
Not difficult with regular expressions !
-
Do a normal selection of your first text part to modify
-
Open the Replace dialog (
Ctrl + H
) -
Untick all box options
-
Tick the
Wrap around
box option -
Select the
Regular expression
search mode -
Enter
(?-s)(?:.+(\R))+.+
in the Find what: zone -
Enter
{\1$0\1}
, in the Replace with: zone -
FIRST possibility :
-
Tick the
In selection
box option -
Click on the
Replace All
button
-
-
SECOND possibility :
-
Do not tick the
In selection
box option -
Click on the
Find Next
button -
Click on the
Replace
button if the present selection must be replaced (A) -
Click again on the
Find Next
button, if the present selection must stay unchanged -
Return to line (A)
-
And so on…
-
=> the FIRST possibility just produce one global replacement on your selection whereas the SECOND possibility allows you to choose the zones of text to modify or not !
Notes :
-
First, the
in-line
modifier ((?-s)
) means that the regex character.
represents a unique standard char only ( not EOL ) -
Then the
(?:.+(\R))+
part represents a bunch of, at least,1
complete line(s), surrounded by a non-capturing group ((?:.......)
), each with :-
1
or more standard characters -
Any kind of End of Line (
\R
), so\r\n
or\n
or\r
, stored as the group1
, which will be re-used in replacement
-
-
Finally the
.+
part match a LAST non-empty line ( Thus, the case if your last section is at the very end of file, without any EOL, will be matched, too ) -
In replacement, we simply write the
{
character first, followed with an EOL character ( the group\1
), followed with the whole regex ($0
) and, again, followed with a second EOL character and, finally, the}
character
Best Regards,
guy038
-