Friday 18 January 2019

Using RegEx in an Editor's Search and Replace

\n is newline

\r is carriage return

^ is start of line

$ end of line or string (a \n comes after the end of the lin: $\n)



Example to find all blank lines:

^\s*$\n

Example replace , newline with , space

Search: ,\n
Replace: ,

When you search for metacharacters such as .[{()\^$|?*+, you need to escape them with backslash (\) so they can be recognized.

Use regex capturing groups and backreferences

In the search field, enter parentheses () that would indicate a capturing group, for example:

\stitle="(.*)?"\s*(/>*)

In the replace field, backreference such groups by numbers starting with 1, for example:

$2<title>$1</title>

Switch the character case in the replace statement

In the replace field, depending on what you want to achieve, for example /U$1.

Enter one of the following syntax:

  • \l changes a character to lowercase until the next character in the string.
    • For example, Bar becomes bar.
  • \u changes a character to uppercase until the next character in the string.
    • For example, bar becomes Bar.
  • \L changes characters to lowercase until the end of the literal string (\E).
    • For example, BAR becomes bar.
  • \U changes characters to uppercase until the end of the literal string (\E).
    • For example, bar becomes BAR.