Difference between revisions of "Regular expressions"

From TED Notepad
Line 81: Line 81:
 
:; {{string|\G}}
 
:; {{string|\G}}
 
:: Matches only at the original starting position. Guarantees that only the position within the document, where the search started, is matched at by this construct. Starting position is usually the one with the caret before the search, also indicated by the {{feature|Status Bar}}.
 
:: Matches only at the original starting position. Guarantees that only the position within the document, where the search started, is matched at by this construct. Starting position is usually the one with the caret before the search, also indicated by the {{feature|Status Bar}}.
 +
 +
=====Alternations=====
 +
 +
:; {{string|<nowiki>|</nowiki>}}
 +
:: Divides pattern alternations. As long as any of the alternations matches, the entire pattern matches.
 +
 +
=====Character classes=====
 +
 +
:; {{string|[}}
 +
:: Opens character class definition. See character class definition below.
  
 
=====Capture groups=====
 
=====Capture groups=====
Line 89: Line 99:
 
:: Ends current capture group. Note: Capture groups can be nested.
 
:: Ends current capture group. Note: Capture groups can be nested.
  
=====Alternations=====
 
  
:; {{string|<nowiki>|</nowiki>}}
+
:; {{string|(?:pattern)}}
:: Divides pattern alternations. As long as any of the alternations matches, the entire pattern matches.
+
:: Cluster group. Cluster groups are non-capturing groups. They act like capturing groups, but do not consume resource for capturing and do not consume capture group numbers for back-referencing.
 +
 
 +
:; {{string|(?<nowiki>|</nowiki>pattern)}}
 +
:: Branch reset cluster group. Inside a branch reset group, capture groups are numbered from the same starting group number in each alternation. Thus several capture groups are assigned the same group number, and then, depending on which alternation actually matches, the number references the correct matching capture group.
 +
 
 +
 
 +
:; {{string|(?&gt;pattern)}}
 +
:: Possesive independent give-nothing-back sub-pattern. This cluster group effectively prevents back-tracking upon matching. Note: Back-tracking is allowed inside of the group before the group ends, but once the group matches as an independent sub-pattern, further back-tracking inside of that group is not performed and the group is ''unmatched'' at that point as a whole. This is like ''grab all you can, and then give nothing back'' operator.
  
=====Character classes=====
+
=====Look-behind assertion=====
  
:; {{string|[}}
+
:; {{string|\K}}
:: Opens character class definition. See character class definition below.
+
:: Removes everything that is to the left of the current matching position from the {{string|\&}} replace back-reference. This effectively provides a look-behind assertion, since it can be used to verify, that a match is preceded by some other pattern.
  
  
Line 104: Line 120:
 
/*  \1..9  back-reference to captured group                        */  
 
/*  \1..9  back-reference to captured group                        */  
 
/*                                                                */  
 
/*                                                                */  
/*                                                                */
 
/*                                                                */
 
/*  \K    removes the match left of the current position from \0  */
 
/*        (this can be used as look-behind assertions in search)  */
 
/*                                                                */
 
/*  (?:pattern)    cluster group (non-capturing group)            */
 
/*  (?>pattern)    independent give nothing back sub-pattern      */
 
/*                                                                */
 
/*  (?|pattern)    branch reset (capture groups are numbered from  */
 
/*                the same starting point in each alternation)    */
 
 
/*                                                                */  
 
/*                                                                */  
 
/*******************************************************************/  
 
/*******************************************************************/  

Revision as of 13:45, 1 August 2011

This section is up to date for TED Notepad version 6.3.1.0.
Basics and escape sequences

Following constructs match specific characters at positions at which they are encountered.

.
Matches any single character (except for newline).


\w
Matches any word character.
\W
Matches any non-word character.
\s
Matches any white-space character.
\S
Matches any non-white-space character.
\d
Matches any digit character.
\D
Matches any non-digit character.


\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.
Zero-length assertions

Following zero-length pattern conditions do not match any specific characters, they only assert that a specific condition is met at the position at which they are encountered.

^
Matches only at line beginnings.
$
Matches only at line ends.
\p
Matches only at paragraph beginnings.
\P
Matches only at paragraph ends.
\A
Matches only at document beginning.
\Z
Matches only at document end.


\b
Matches only at word boundary, i.e. one of the characters around the current matching position must be a word character and the other may not.
\B
Matches only inside a word, i.e. both characters around the current matching position must be word characters.
\y
Matches only at word beginning, i.e. the second of the characters around the current matching position must be a word character and the first one may not.
\Y
Matches only at word end, i.e. the first of the characters around the current matching position must be a word character and the second one may not.


\G
Matches only at the original starting position. Guarantees that only the position within the document, where the search started, is matched at by this construct. Starting position is usually the one with the caret before the search, also indicated by the Status Bar.
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.
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.


(?:pattern)
Cluster group. Cluster groups are non-capturing groups. They act like capturing groups, but do not consume resource for capturing and do not consume capture group numbers for back-referencing.
(?|pattern)
Branch reset cluster group. Inside a branch reset group, capture groups are numbered from the same starting group number in each alternation. Thus several capture groups are assigned the same group number, and then, depending on which alternation actually matches, the number references the correct matching capture group.


(?>pattern)
Possesive independent give-nothing-back sub-pattern. This cluster group effectively prevents back-tracking upon matching. Note: Back-tracking is allowed inside of the group before the group ends, but once the group matches as an independent sub-pattern, further back-tracking inside of that group is not performed and the group is unmatched at that point as a whole. This is like grab all you can, and then give nothing back operator.
Look-behind assertion
\K
Removes everything that is to the left of the current matching position from the \& replace back-reference. This effectively provides a look-behind assertion, since it can be used to verify, that a match is preceded by some other pattern.



More escape sequences

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.