Difference between revisions of "Regular expressions"

From TED Notepad
Line 21: Line 21:
 
:; {{string|[}}
 
:; {{string|[}}
 
:: Opens character class definition. See character class definition below.
 
:: Opens character class definition. See character class definition below.
 
  
  
Line 27: Line 26:
 
<!--
 
<!--
 
/*                                                                */  
 
/*                                                                */  
/*  \\    escape for \                                            */
+
 
/*  \^    escape for ^                                            */
 
/*  \$    escape for $                                            */
 
/*  \.    escape for .                                            */
 
/*  \|    escape for |                                            */
 
/*  \(    escape for (                                            */
 
/*  \)    escape for )                                            */
 
/*  \[    escape for [                                            */
 
/*  \]    escape for ]                                            */
 
/*  \*    escape for *                                            */
 
/*  \+    escape for +                                            */
 
/*  \?    escape for ?                                            */
 
/*  \{    escape for {                                            */
 
/*  \}    escape for }                                            */
 
/*  \<    escape for <                                            */
 
/*  \>    escape for >                                            */
 
/*  \:    escape for :                                            */
 
 
/*                                                                */  
 
/*                                                                */  
 
/*  \&    reserved for empty string (this is not implemented!)    */  
 
/*  \&    reserved for empty string (this is not implemented!)    */  
Line 191: Line 174:
  
 
-->
 
-->
 +
 +
=====Search pattern escapes=====
 +
 +
Since many characters have special meanings in regular expressions, escapes are provided to allow using these characters in searches.
 +
 +
:; {{string|\\}}
 +
:: Matches character {{string|\}}. Note: Unescaped single {{string|\}} has a special meaning.
 +
:; {{string|\^}}
 +
:: Matches character {{string|^}}. Note: Unescaped single {{string|^}} has a special meaning.
 +
:; {{string|\$}}
 +
:: Matches character {{string|$}}. Note: Unescaped single {{string|$}} has a special meaning.
 +
:; {{string|\.}}
 +
:: Matches character {{string|.}}. Note: Unescaped single {{string|.}} has a special meaning.
 +
:; {{string|\<nowiki>|</nowiki>}}
 +
:: Matches character {{string|<nowiki>|</nowiki>}}. Note: Unescaped single {{string|<nowiki>|</nowiki>}} has a special meaning.
 +
:; {{string|\(}}
 +
:: Matches character {{string|(}}. Note: Unescaped single {{string|(}} has a special meaning.
 +
:; {{string|\)}}
 +
:: Matches character {{string|)}}. Note: Unescaped single {{string|)}} has a special meaning.
 +
:; {{string|\[}}
 +
:: Matches character {{string|[}}. Note: Unescaped single {{string|[}} has a special meaning.
 +
:; {{string|\]}}
 +
:: Matches character {{string|]}}. Note: Unescaped single {{string|]}} has a special meaning.
 +
:; {{string|\*}}
 +
:: Matches character {{string|*}}. Note: Unescaped single {{string|*}} has a special meaning.
 +
:; {{string|\+}}
 +
:: Matches character {{string|+}}. Note: Unescaped single {{string|+}} has a special meaning.
 +
:; {{string|\?}}
 +
:: Matches character {{string|?}}. Note: Unescaped single {{string|?}} has a special meaning.
 +
:; {{string|\<nowiki>{</nowiki>}}
 +
:: Matches character {{string|<nowiki>{</nowiki>}}. Note: Unescaped single {{string|<nowiki>{</nowiki>}} has a special meaning.
 +
:; {{string|\<nowiki>}</nowiki>}}
 +
:: Matches character {{string|<nowiki>}</nowiki>}}. Note: Unescaped single {{string|<nowiki>}</nowiki>}} has a special meaning.
 +
:; {{string|\&lt;}}
 +
:: Matches character {{string|&lt;}}. Note: Unescaped single {{string|&lt;}} has a special meaning.
 +
:; {{string|\&gt;}}
 +
:: Matches character {{string|&gt;}}. Note: Unescaped single {{string|&gt;}} has a special meaning.
 +
:; {{string|\:}}
 +
:: Matches character {{string|:}}. Note: Unescaped single {{string|:}} has a special meaning.
  
 
=====Replace patterns=====
 
=====Replace patterns=====

Revision as of 12:47, 1 August 2011

Search patterns

Any of these constructs may appear anywhere in the search pattern, as long as regular expressions are turned on.

^
Matches only at line beginnings. This construct does not match any specific characters, but succeeds if the matching cursor is at line beginning, and fails in all other cases.
$
Matches only at line ends. This construct does not match any specific characters, but succeeds if the matching cursor is at line end, and fails in all other cases.
.
Matches any single character (except for newline).
(
Begins a new capture group. Capture groups are useful for back-references in both search and replace patterns.
)
Ends current capture group. Note: Capture groups can be nested.
|
Divides pattern alternations. As long as any of the alternations matches, the entire pattern matches.
[
Opens character class definition. See character class definition below.



Search pattern escapes

Since many characters have special meanings in regular expressions, escapes are provided to allow using these characters in searches.

\\
Matches character \. Note: Unescaped single \ has a special meaning.
\^
Matches character ^. Note: Unescaped single ^ has a special meaning.
\$
Matches character $. Note: Unescaped single $ has a special meaning.
\.
Matches character .. Note: Unescaped single . has a special meaning.
\|
Matches character |. Note: Unescaped single | has a special meaning.
\(
Matches character (. Note: Unescaped single ( has a special meaning.
\)
Matches character ). Note: Unescaped single ) has a special meaning.
\[
Matches character [. Note: Unescaped single [ has a special meaning.
\]
Matches character ]. Note: Unescaped single ] has a special meaning.
\*
Matches character *. Note: Unescaped single * has a special meaning.
\+
Matches character +. Note: Unescaped single + has a special meaning.
\?
Matches character ?. Note: Unescaped single ? has a special meaning.
\{
Matches character {. Note: Unescaped single { has a special meaning.
\}
Matches character }. Note: Unescaped single } has a special meaning.
\<
Matches character <. Note: Unescaped single < has a special meaning.
\>
Matches character >. Note: Unescaped single > has a special meaning.
\:
Matches character :. Note: Unescaped single : has a special meaning.
Replace patterns

Any of these constructs may appear anywhere in the replace pattern, as long as regular expressions are turned on.

\\
Inserts a backslash.
\n
Inserts a newline sequence (CR, NL or CR/NL; depends on current document options).
\t
Inserts a horizontal tab character (TAB).
\f
Inserts a form feed character (FF).
\a
Inserts a vell character (BEL).
\v
Inserts a vertical tab (VT).
\e
Inserts an escape character (ESC).
\0
Inserts a null character (NUL).
\xhh
Inserts a character in hex notation.
\uhhhh
Inserts a character in unicode notation (unicode version only).
\cA
Inserts a character in control notation.
\Q ... \E
Quoted string. Anything between \Q and \E is treated as plain-text string.
\&
Back-reference to the entire match.
\1, \2, ..., \9
Back-reference to a specific captured group.
\+
Back-reference to the last successfull captured group. Consider having several alternations, each with a group inside it. Only one of the alternations will match, thus only one of those groups will be valid upon replacing. This back-reference allows referencing the correct one of those groups, based on which of the alternations matched.
  • Note: This can also be achieved by using branch restart groups.