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 aroundbox option -
Select the
Regular expressionsearch mode -
Enter
(?-s)(?:.+(\R))+.+in the Find what: zone -
Enter
{\1$0\1}, in the Replace with: zone -
FIRST possibility :
-
Tick the
In selectionbox option -
Click on the
Replace Allbutton
-
-
SECOND possibility :
-
Do not tick the
In selectionbox option -
Click on the
Find Nextbutton -
Click on the
Replacebutton if the present selection must be replaced (A) -
Click again on the
Find Nextbutton, 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-linemodifier ((?-s)) means that the regex character.represents a unique standard char only ( not EOL ) -
Then the
(?:.+(\R))+part represents a bunch of, at least,1complete line(s), surrounded by a non-capturing group ((?:.......)), each with :-
1or more standard characters -
Any kind of End of Line (
\R), so\r\nor\nor\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
-