Debian Codegraph @

easel/esl_regexp.md

raw symbols SCIP
1 # esl_regexp - regular expression matching
2
3 The `regexp` module contains portable functions for using regular
4 expressions to match and parse strings.
5
6 There are many different regular expression syntaxes. Easel
7 implements a small regular expression machine with a limited syntax,
8 allowing the most familiar and important regular expression
9 operators. It takes advantage of a compact, public domain regular
10 expression engine written by Henry Spencer at the University of
11 Toronto. Easel's regular expressions are not as powerful as the
12 regular expression syntax in the Perl language, for example, but
13 they're sufficient for many useful parsing needs in a C application.
14
15 ## functions
16
17 | Function | Synopsis |
18 |--------------------------------|------------------------------------------------------|
19 | `esl_regexp_Create()` | Create a new `ESL_REGEXP` |
20 | `esl_regexp_Destroy()` | Destroy an `ESL_REGEXP` |
21 | `esl_regexp_Match()` | Find first match of a pattern in a string |
22 | `esl_regexp_Compile()` | Precompile a pattern |
23 | `esl_regexp_MultipleMatches()` | Find next match of a precompiled pattern in a string |
24 | `esl_regexp_SubmatchDup()` | Extract a (sub)match as newly allocated string |
25 | `esl_regexp_SubmatchCopy()` | Extract a (sub)match string, copy to allocated space |
26 | `esl_regexp_SubmatchCoords()` | Extract start/end coords of a (sub)match |
27
28
29 ## examples
30
31 The trickiest bit of using `esl_regexp` is writing your pattern,
32 because of all the confusing backslashing. The `esl_regexp` module
33 provides three example drivers. Their code provides a template for
34 your own code, and running them lets you test your pattern against
35 different strings.
36
37 `esl_regexp_example '<pattern>' <string>` uses `esl_regexp_Match()` to
38 find the first match in `string`, if any, and prints its (0-offset)
39 coordinates.
40
41 `esl_regexp_example2 '<pattern>' <string>` uses `esl_regexp_MultipleMatches()` to
42 find one or more matches in `string`, and prints their (0-offset) coords.
43
44 `esl_regexp_example3 '<pattern>' <string> <ntok>` tests patterns using
45 `()` to demarcate specific tokens to extract; for each token it prints
46 the (0-offset) coords and the token's substring.
47
48 Put the `<pattern>` argument in single quotes on the command
49 line. Single quotes around a shell command argument blocks
50 metacharacter interpretation. See the `QUOTING` section of `man bash`
51 for more information (including on what to do if you need a single
52 quote `'` in your pattern). Use `echo` to see how the shell digested
53 your pattern.
54
55 When you put a pattern in a C string in your code, escape all the
56 backslash characters: replace `\` with `\\`. In C, `\` is interpreted
57 as the beginning of an
58 [escape sequence](https://en.wikipedia.org/wiki/Escape_sequences_in_C).
59
60
61 ## syntax
62
63 There are 11 metacharacters
64 `|?*+[().^$\` that encode regular expression operations.
65
66 `.` is the ANY operator. It matches any single character.
67
68 `?`, `*`, and `+` are repetition operators that
69 follow some pattern atom. `?` means 0 or 1 occurrences of
70 the atom; `*` means 0 or more; `+` means 1 or more. For
71 example, `foo?` matches fo and foo; `foo*` matches fo,
72 foo, fooo, foooo, and so on; `foo+` matches foo, fooo, foooo, and so on.
73
74 `^` is the beginning-of-string anchor; `$` is the end-of-string anchor.
75
76 `|` is the concatenation operator, specifying alternative ways
77 to match. For example, `foo|bar|baz` matches baz, bar, or foo;
78 `(foo|bar|baz)+` matches barfoobaz, foofoofoo, etc.
79
80 `()` are for grouping and tokenizing. Anything inside `()`
81 is grouped and treated as a single atom for purposes of a subsequent
82 `?*+` operator, as in the `(foo|bar|baz)+` example above.
83 Anything inside `()` becomes a token, extractable by
84 `_Submatch*` functions.
85
86 `\`, when followed by any metacharacter (or in fact, any
87 non-alphanumeric character), specifies that that character should be
88 treated literally (as an ordinary character). For example, the
89 pattern `\\c:` matches the string `\c:`.
90
91 A backslash followed by an alphanumeric character is either an
92 **escape character** or a **character set**. Four escape
93 characters are recognized: `\f` (form feed), `\n` (newline),
94 `\r` (carriage return), and `\t` (TAB). Six character set
95 codes are recognized, with the same meanings they have in Perl regular
96 expressions:
97
98 | code | meaning | equivalent to |
99 |------|----------------------|-----------------|
100 | `\d` | digit | `[0-9]` |
101 | `\D` | not a digit | `[^0-9]` |
102 | `\w` | word character | `[0-9a-z_a-Z]` |
103 | `\W` | not a word character | `[^0-9a-z_a-Z]` |
104 | `\s` | whitespace | `[ \t\n\r\f]` |
105 | `\S` | not whitespace | `[^ \t\n\r\f]` |
106
107
108 A backslash followed by an alphanumeric character that is neither an
109 escape code or a character set code is an error.
110
111 `[` is the set (or range) operator. (An unmatched `]` is not a
112 metacharacter, but a `[` metacharacter always implies a range and
113 always must have a closing `]`.) The set of characters inside brackets
114 `[]` are read as a single ANYOF atom. A set may be specified as a
115 range of ASCII characters; `[a-z]`, for example, means any lower-case
116 character from a to z, `[a-zA-Z]` means any alphabetic character, and
117 `[0-9]` means any digit. For example, `fo[ox]` matches foo or
118 fox. Additionally, `[^` implies the opposite, an ANYBUT atom: any
119 character _except_ the set of characters named is a match. For
120 example, `foo[^ ]+` matches `football` in the string `football game`.
121
122 Metacharacters are handled differently inside the `[]` or `[^]` range
123 operators. The only special characters are `]`, `-`, and `\`. A `]`
124 character indicates the end of the range operator unless it
125 immediately follows the `[`, in which case it is treated as a normal
126 character (thus, weirdly, `[][{}()]` will match any open/close
127 brace/parenthesis character). The `-` character indicates the middle
128 of a three-character `x-y` ASCII range, unless it comes at the
129 beginning or end of the range operator (thus `[]-]` recognizes either
130 `]` or `-` as literals). The `\` character indicates an escaped
131 character, and only five such escape characters are recognized inside
132 a range operator: `\f` (form feed), `\n` (newline), `\r` (carriage
133 return), `\t` (TAB), and `\\` (backslash itself). Character set codes
134 like `\s+` are not allowed within range operators.
135
136
137
138
139
140
141
142
143