IDE regular expressions: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: IDE regular expressions
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Normal regular expression | |
| 2 | + | ||
| 3 | + | Metacharacters: | |
| 4 | + | ||
| 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]` | |
| 13 | + | ||
| 14 | + | Quantifiers: | |
| 15 | + | ||
| 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` | |
| 22 | + | ||
| 23 | + | 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}. | |
| 24 | + | ||
| 25 | + | 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 \`\`?''. | |
| 26 | + | ||
| 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` | |
| 33 | + | ||
| 34 | + | ## Simple Syntax | |
| 35 | + | ||
| 36 | + | Some IDE dialogs provide a checkbox to choose between *simple syntax* and *regular expressions*. The *simple syntax* is often shorter for common file name filters. | |
| 37 | + | ||
| 38 | + | Technically it does this: | |
| 39 | + | ||
| 40 | + | The following characters are replaced with | |
| 41 | + | . -> \. | |
| 42 | + | * -> .* | |
| 43 | + | ? -> . | |
| 44 | + | , -> | | |
| 45 | + | ; -> | | |
| 46 | + | ||
| 47 | + | Finally enclose by ^( )$ | |
| 48 | + | ||
| 49 | + | ### Examples for simple syntax | |
| 50 | + | ||
| 51 | + | - *.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. | |
| 52 | + | - *.pas;*.pp;*p - matches all files like unit1.pas, 678.pp or %.p | |
| 53 | + | - unit*.pas - matches all files beginning with *unit* and ending with *.pas* like unit1.pas and unit.pas. | |
| 54 | + | - 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. | |
| 55 | + | ||
| 56 | + | ## Quoted symbols | |
| 57 | + | ||
| 58 | + | The backslash '\\' can be used to treat special characters like '.' as normal character or to get some special ranges. | |
| 59 | + | ||
| 60 | + | \. matches a point '.' | |
| 61 | + | \d matches a number character '0'..'9' | |
| 62 | + | \D matches a non number character | |
| 63 | + | \s matches a space character ' ',#9,#10,#12,#13 | |
| 64 | + | \S matches a non space character | |
| 65 | + | \w matches a word character 'a'..'z','A'..'Z','0'..'9','_' | |
| 66 | + | \W matches a non word character | |
| 67 | + | \b word boundary | |
| 68 | + | \B not word boundary | |
| 69 | + | \A begin of line | |
| 70 | + | \Z end of line | |
| 71 | + | ||
| 72 | + | ## Search and Replace with regular expressions | |
| 73 | + | ||
| 74 | + | The find dialogs support regular expressions for finding and replacing. Each found pattern grouped in round brackets can be used by a \$1, ..., \$9 variable | |
| 75 | + | ||
| 76 | + | Example 1: | |
| 77 | + | ||
| 78 | + | - Find expression: a(.*)c | |
| 79 | + | - Replace expression: A\$1C | |
| 80 | + | - Text: 'abc aLazc' | |
| 81 | + | ||
| 82 | + | The \$1 will be replaced with the found text, that matches the first bracket. | |
| 83 | + | ||
| 84 | + | - Result: 'AbC ALazC' | |
| 85 | + | ||
| 86 | + | Example 2: | |
| 87 | + | ||
| 88 | + | - Find expression: T(prefix)(\w+) | |
| 89 | + | - Replace expression: T\$2Suffix | |
| 90 | + | - Text: replaced from 'TprefixClass' to 'TClassSuffix' | |
| 91 | + | ||
| 92 | + | You can also use the following special chars in the *replace* term | |
| 93 | + | ||
| 94 | + | \n | |
| 95 | + | Inserts a Linebreak | |
| 96 | + | ||
| 97 | + | \l \u | |
| 98 | + | Lower 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 | |
| 99 | + | ||
| 100 | + | \L \U | |
| 101 | + | Lower or Uppercase the *full* text of the next \$n. The \$n must follow immediately | |
| 102 | + | ||
| 103 | + | --- | |
| 104 | + | *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).* |
