Difference between revisions of "Regular expressions"

From TED Notepad
Line 26: Line 26:
 
:; {{string|\0}}
 
:; {{string|\0}}
 
:: Matches a null character (NUL).
 
:: Matches a null character (NUL).
 +
 +
:; {{string|\x}}''hh''
 +
:: Matches a character in hex notation.
 +
:; {{string|\u}}''hhhh''
 +
:: Matches a character in unicode notation (unicode version only).
 +
:; {{string|\c}}''A''
 +
:: Matches a character in control notation.
 +
 +
:; {{string|\Q}} ''...'' {{string|\E}}
 +
:: Quoted string. Anything between {{string|\Q}} and {{string|\E}} is treated as plain-text string and is matched exactly as it appears in the pattern.
  
 
=====Capture groups=====
 
=====Capture groups=====
Line 47: Line 57:
  
 
<!--
 
<!--
/*                                                                */
 
 
/*                                                                */
 
/*                                                                */
 
/*  \x    character in hex notation                              */
 
/*  \u    character in unicode notation (unicode version only)    */
 
/*  \c    character in control notation                          */
 
/*                                                                */
 
/*  \Q    quoted string until \E                                  */
 
/*                                                                */
 
 
/*  \1..9  back-reference to captured group                        */  
 
/*  \1..9  back-reference to captured group                        */  
 
/*                                                                */  
 
/*                                                                */  
Line 87: Line 87:
 
/*  (?|pattern)    branch reset (capture groups are numbered from  */  
 
/*  (?|pattern)    branch reset (capture groups are numbered from  */  
 
/*                the same starting point in each alternation)    */  
 
/*                the same starting point in each alternation)    */  
/*                                                                */
 
/*******************************************************************/
 
/* Reserved constructs in pattern. Not implemented yet.            */
 
/*                                                                */
 
/*  \R    line break (newlines, page breaks, vertical tabs, etc.) */
 
/*                                                                */
 
/*  (?<name>...)  named capturing group                          */
 
/*  \k<name>      named back-reference                            */
 
/*                                                                */
 
/*  (?=pattern)    zero-width positive look-ahead assertion        */
 
/*  (?!pattern)    zero-width negative look-ahead assertion        */
 
/*                                                                */
 
/*  (?R)          recurse to the beginning of the pattern        */
 
/*  (?0)          recurse to the beginning of the pattern        */
 
/*  (?1..9)        recurse to the given capturing group            */
 
/*  (?&<name>)    recurse to the given named capturing group      */
 
/*                                                                */
 
/*  (*PRUNE)      prevents back-tracking back from this point    */
 
/*  (*SKIP)        like (*PRUNE) but also forwards the engine here */
 
/*  (*FAIL)        fails at given position, forcing to back-track  */
 
/*  (*ACCEPT)      terminates and causes successful match here    */
 
/*                                                                */
 
/*  (?(condition)yes|no)  conditional expression                  */
 
 
/*                                                                */  
 
/*                                                                */  
 
/*******************************************************************/  
 
/*******************************************************************/  
Line 257: Line 234:
  
 
:; {{string|\Q}} ''...'' {{string|\E}}
 
:; {{string|\Q}} ''...'' {{string|\E}}
:: Quoted string. Anything between {{string|\Q}} and {{string|\E}} is treated as plain-text string.
+
:: Quoted string. Anything between {{string|\Q}} and {{string|\E}} is treated as plain-text string and is inserted exactly as it appears in the pattern.
  
 
:; {{string|\&}}
 
:; {{string|\&}}

Revision as of 12:55, 1 August 2011

This section is up to date for TED Notepad version 6.3.1.0.
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).
\n
Matches one newline sequence (CR, NL or CR/NL)
\t
Matches a horizontal tab character (TAB).
\f
Matches a form feed character (FF).
\a
Matches a bell character (BEL).
\v
Matches a vertical tab (VT).
\e
Matches an escape character (ESC).
\0
Matches a null character (NUL).
\xhh
Matches a character in hex notation.
\uhhhh
Matches a character in unicode notation (unicode version only).
\cA
Matches a character in control notation.
\Q ... \E
Quoted string. Anything between \Q and \E is treated as plain-text string and is matched exactly as it appears in the pattern.
Capture groups
(
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.
Alternations
|
Divides pattern alternations. As long as any of the alternations matches, the entire pattern matches.
Character classes
[
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 and is inserted exactly as it appears in the pattern.
\&
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.