Difference between revisions of "IDE regular expressions"

From Free Pascal wiki
Jump to navigationJump to search
Line 3: Line 3:
 
They are pretty similar to Perl regular expression syntax.
 
They are pretty similar to Perl regular expression syntax.
  
=== Point . ===
+
Metacharacters:
 +
    \  Quote the next metacharacter
 +
    ^  Match the beginning of the line
 +
    .   Match any character (except newline). Example: 'a.c' matches 'abc', 'aBC', 'axc', 'a3c', 'a$c', etc.
 +
    $  Match the end of the line (or before newline at the end)
 +
    |  Alternation
 +
    ()  Grouping. For example: (abc)+ matches 'abc' or 'abcabc' or 'abcabcabc' etc.
 +
    []  Character class
  
Matches any one character. Example: 'a.c' matches 'abc', 'aBC', 'axc', 'a3c', 'a$c', etc.
+
Quantifiers:
 +
    *      Match 0 or more times
 +
    +      Match 1 or more times
 +
    ?      Match 1 or 0 times
 +
    {n}    Match exactly n times
 +
    {n,}  Match at least n times
 +
    {n,m}  Match at least n but not more than m times
  
=== Round brackets ( ) ===
+
Curly brackets in any other context is treated as a regular character.
 +
The ``*'' is equivalent to {0,}, the ``+'' to {1,} and the ``?'' to {0,1}.
  
Useful to bound expressions. For example: (abc)+ matches 'abc' or 'abcabc' or 'abcabcabc' etc.
+
By default, a quantifier is ''greedy'', that means, it will match as many times as possible. To match the minimum number of times possible, append a ``?''.
 +
 
 +
    *?    Match 0 or more times
 +
    +?    Match 1 or more times
 +
    ??    Match 0 or 1 time
 +
    {n}?  Match exactly n times
 +
    {n,}?  Match at least n times
 +
    {n,m}? Match at least n but not more than m times
  
 
== Simple Syntax ==
 
== Simple Syntax ==

Revision as of 11:31, 26 July 2006

Normal regular expression

They are pretty similar to Perl regular expression syntax.

Metacharacters:

   \   Quote the next metacharacter
   ^   Match the beginning of the line
   .   Match any character (except newline). Example: 'a.c' matches 'abc', 'aBC', 'axc', 'a3c', 'a$c', etc.
   $   Match the end of the line (or before newline at the end)
   |   Alternation
   ()  Grouping. For example: (abc)+ matches 'abc' or 'abcabc' or 'abcabcabc' etc.
   []  Character class

Quantifiers:

   *      Match 0 or more times
   +      Match 1 or more times
   ?      Match 1 or 0 times
   {n}    Match exactly n times
   {n,}   Match at least n times
   {n,m}  Match at least n but not more than m times

Curly brackets in any other context is treated as a regular character. The ``* is equivalent to {0,}, the ``+ to {1,} and the ``? to {0,1}.

By default, a quantifier is greedy, that means, it will match as many times as possible. To match the minimum number of times possible, append a ``?.

   *?     Match 0 or more times
   +?     Match 1 or more times
   ??     Match 0 or 1 time
   {n}?   Match exactly n times
   {n,}?  Match at least n times
   {n,m}? Match at least n but not more than m times

Simple Syntax

Some IDE dialogs provide a checkbox to enable 'simple syntax'. These regular expressions are shorter for common file name filters.

Technically it does this:

  The following characters are replaced with
  . -> \.
  * -> .*
  ? -> .
  , -> |
  ; -> |
  
  Finally enclose by ^( )$


Search and Replace with regular expressions

The find dialogs support regular expressions for finding and replacing. For example:

  • Find expression: a(.*)c
  • Replace expression: A$1C
  • Text: 'abc aLazc'

The $1 will be replaced with the found text, that matches the first bracket.

  • Result: 'AbC ALazC'