Sign in Join
Wiki copytemporary

IDE regular expressions: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Normal regular expression
22
33Metacharacters:
44
5-`   \   Quote the next metacharacter`\
6-`   ^   Match the beginning of the line`\
7-`   .   Match any character (except newline). Example: 'a.c' matches 'abc', 'aBC', 'axc', 'a3c', 'a$c', etc.`\
8-`   $   Match the end of the line (or before newline at the end)`\
9-`   |   Alternation`\
10-`   ()  Grouping. For example: (abc)+ matches 'abc' or 'abcabc' or 'abcabcabc' etc.`\
11-`   []  Character class. For example [abc], [a-z], [a-zA-Z0-9_]`\
12-`   [^] Reversed character class. For example [^a-z]`
5+``` pascal
6+ \ Quote the next metacharacter
7+ ^ Match the beginning of the line
8+ . Match any character (except newline). Example: 'a.c' matches 'abc', 'aBC', 'axc', 'a3c', 'a$c', etc.
9+ $ Match the end of the line (or before newline at the end)
10+ | Alternation
11+ () Grouping. For example: (abc)+ matches 'abc' or 'abcabc' or 'abcabcabc' etc.
12+ [] Character class. For example [abc], [a-z], [a-zA-Z0-9_]
13+ [^] Reversed character class. For example [^a-z]
14+```
1315
1416Quantifiers:
1517
16-`   *      Match 0 or more times`\
17-`   +      Match 1 or more times`\
18-`   ?      Match 1 or 0 times`\
19-`   {n}    Match exactly n times`\
20-`   {n,}   Match at least n times`\
21-`   {n,m}  Match at least n but not more than m times`
18+```text
19+ * Match 0 or more times
20+ + Match 1 or more times
21+ ? Match 1 or 0 times
22+ {n} Match exactly n times
23+ {n,} Match at least n times
24+ {n,m} Match at least n but not more than m times
25+```
2226
2327Curly brackets in any other context is treated as a regular character. The \`\`**is equivalent to {0,}, the \`\`+* to {1,} and the \`\`?'' to {0,1}.
2428
2529By 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 \`\`?''.
2630
27-`   *?     Match 0 or more times`\
28-`   +?     Match 1 or more times`\
29-`   ??     Match 0 or 1 time`\
30-`   {n}?   Match exactly n times`\
31-`   {n,}?  Match at least n times`\
32-`   {n,m}? Match at least n but not more than m times`
31+```text
32+ *? Match 0 or more times
33+ +? Match 1 or more times
34+ ?? Match 0 or 1 time
35+ {n}? Match exactly n times
36+ {n,}? Match at least n times
37+ {n,m}? Match at least n but not more than m times
38+```
3339
3440## Simple Syntax
3541
3642Some IDE dialogs provide a checkbox to choose between *simple syntax* and *regular expressions*. The *simple syntax* is often shorter for common file name filters.
3743
3844Technically it does this:
3945
4046 The following characters are replaced with
4147 . -> \.
4248 * -> .*
4349 ? -> .
4450 , -> |
4551 ; -> |
4652
4753 Finally enclose by ^( )$
4854
4955### Examples for simple syntax
5056
5157- *.pas - matches all file names ending with .pas, like Unit1.pas or Some.txt.pas. Default is to match case sensitive. So it does not fit unit1.PAS.
5258- *.pas;*.pp;*p - matches all files like unit1.pas, 678.pp or %.p
5359- unit*.pas - matches all files beginning with *unit* and ending with *.pas* like unit1.pas and unit.pas.
5460- unit?.pas - matches all files beginning with *unit*, ending with *.pas* and exactly one like arbitrary character in between like unit1.pas and unit2.pas.
5561
5662## Quoted symbols
5763
5864The backslash '\\' can be used to treat special characters like '.' as normal character or to get some special ranges.
5965
6066 \. matches a point '.'
6167 \d matches a number character '0'..'9'
6268 \D matches a non number character
6369 \s matches a space character ' ',#9,#10,#12,#13
6470 \S matches a non space character
6571 \w matches a word character 'a'..'z','A'..'Z','0'..'9','_'
6672 \W matches a non word character
6773 \b word boundary
6874 \B not word boundary
6975 \A begin of line
7076 \Z end of line
7177
7278## Search and Replace with regular expressions
7379
7480The find dialogs support regular expressions for finding and replacing. Each found pattern grouped in round brackets can be used by a \$1, ..., \$9 variable
7581
7682Example 1:
7783
7884- Find expression: a(.*)c
7985- Replace expression: A\$1C
8086- Text: 'abc aLazc'
8187
8288The \$1 will be replaced with the found text, that matches the first bracket.
8389
8490- Result: 'AbC ALazC'
8591
8692Example 2:
8793
8894- Find expression: T(prefix)(\w+)
8995- Replace expression: T\$2Suffix
9096- Text: replaced from 'TprefixClass' to 'TClassSuffix'
9197
9298You can also use the following special chars in the *replace* term
9399
94100\n
95101Inserts a Linebreak
96102
97103\l \u
98104Lower or Uppercases the *next* Char. Use this in front of \$n. \u\$1 Will insert the match from the first brackets, but with its first char made uppercase
99105
100106\L \U
101107Lower or Uppercase the *full* text of the next \$n. The \$n must follow immediately
102108
103109---
104110*From the Free Pascal wiki, page "IDE regular expressions", as it stood on 2022-02-05 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history