Debian Codegraph @

info/gcl-si/Regular-Expressions.html

raw ◀ ▶
1 Debian patch touch this file
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html>
•3 <!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>Regular Expressions (GCL SI Manual)</title>
7
8 <meta name="description" content="Regular Expressions (GCL SI Manual)">
9 <meta name="keywords" content="Regular Expressions (GCL SI Manual)">
10 <meta name="resource-type" content="document">
11 <meta name="distribution" content="global">
12 <meta name="Generator" content="makeinfo">
•13 <meta name="viewport" content="width=device-width,initial-scale=1">
14
15 <link href="index.html" rel="start" title="Top">
16 <link href="Function-and-Variable-Index.html" rel="index" title="Function and Variable Index">
•17 <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
18 <link href="System-Definitions.html" rel="up" title="System Definitions">
19 <link href="System-Definitions.html" rel="prev" title="System Definitions">
20 <style type="text/css">
21 <!--
•22 a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
23 a.summary-letter {text-decoration: none}
24 blockquote.indentedblock {margin-right: 0em}
25 div.display {margin-left: 3.2em}
26 div.example {margin-left: 3.2em}
27 kbd {font-style: oblique}
28 pre.display {font-family: inherit}
29 pre.format {font-family: inherit}
30 pre.menu-comment {font-family: serif}
31 pre.menu-preformatted {font-family: serif}
32 span.nolinebreak {white-space: nowrap}
33 span.roman {font-family: initial; font-weight: normal}
34 span.sansserif {font-family: sans-serif; font-weight: normal}
•35 span:hover a.copiable-anchor {visibility: visible}
36 ul.no-bullet {list-style: none}
37 -->
38 </style>
39
40
41 </head>
42
43 <body lang="en">
•44 <div class="section" id="Regular-Expressions">
45 <div class="header">
46 <p>
•47 Previous: <a href="System-Definitions.html" accesskey="p" rel="prev">System Definitions</a>, Up: <a href="System-Definitions.html" accesskey="u" rel="up">System Definitions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Function-and-Variable-Index.html" title="Index" rel="index">Index</a>]</p>
48 </div>
49 <hr>
50 <span id="Regular-Expressions-1"></span><h3 class="section">17.1 Regular Expressions</h3>
51
52 <p>The function <code>string-match</code> (*Index string-match::) is used to
53 match a regular expression against a string. If the variable
54 <code>*case-fold-search*</code> is not nil, case is ignored in the match.
55 To determine the extent of the match use *Index match-beginning:: and
56 *Index match-end::.
57 </p>
58 <p>Regular expressions are implemented using Henry Spencer&rsquo;s package
59 (thank you Henry!), and much of the description of regular expressions
60 below is copied verbatim from his manual entry. Code for delimited
61 searches, case insensitive searches, and speedups to allow fast
62 searching of long files was contributed by W. Schelter. The speedups
63 use an adaptation by Schelter of the Boyer and Moore string search
64 algorithm to the case of branched regular expressions. These allow
65 such expressions as &rsquo;not_there|really_not&rsquo; to be searched for 30 times
66 faster than in GNU emacs (1995), and 200 times faster than in the
67 original Spencer method. Expressions such as [a-u]bcdex get a speedup
68 of 60 and 194 times respectively. This is based on searching a string
69 of 50000 characters (such as the file tk.lisp).
70 </p>
71 <ul>
72 <li> A regular expression is a string containing zero or more <i>branches</i> which are separated by <code>|</code>. A match of the regular expression against a string is simply a match of the string with one of the branches.
73 </li><li> Each branch consists of zero or more <i>pieces</i>, concatenated. A matching
74 string must contain an initial substring matching the first piece, immediately
75 followed by a second substring matching the second piece and so on.
76 </li><li> Each piece is an <i>atom</i> optionally followed by <code>+</code>, <code>*</code>, or <code>?</code>.
77 </li><li> An atom followed by <code>+</code> matches a sequence of 1 or more matches of the atom.
78 </li><li> An atom followed by <code>*</code> matches a sequence of 0 or more matches of the atom.
79 </li><li> An atom followed by <code>?</code> matches a match of the atom, or the null string.
80 </li><li> An atom is
81 <ul class="no-bullet">
82 <li>- a regular expression in parentheses matching a match for the regular expression
83 </li><li>- a <i>range</i> see below
84 </li><li>- a <code>.</code> matching any single character
85 </li><li>- a <code>^</code> matching the null string at the beginning of the input string
86 </li><li>- a <code>$</code> matching the null string at the end of the input string
87 </li><li>- a <code>\</code> followed by a single character matching that character
88 </li><li>- a single character with no other significance
89 (matching that character).
90 </li></ul>
91 </li><li> A <i>range</i> is a sequence of characters enclosed in <code>[]</code>.
92 It normally matches any single character from the sequence.
93 <ul class="no-bullet">
94 <li>- If the sequence begins with <code>^</code>,
95 it matches any single character <i>not</i> from the rest of the sequence.
96 </li><li>- If two characters in the sequence are separated by <code>-</code>, this is shorthand
97 for the full list of ASCII characters between them
98 (e.g. <code>[0-9]</code> matches any decimal digit).
99 </li><li>- To include a literal <code>]</code> in the sequence, make it the first character
100 (following a possible <code>^</code>).
101 </li><li>- To include a literal <code>-</code>, make it the first or last character.
102 </li></ul>
103 </li></ul>
104
•105 <ul class="section-toc">
106 <li><a href="#Ordering-Multiple-Matches" accesskey="1">Ordering Multiple Matches</a></li>
107 </ul>
108 <div class="unnumberedsubsec" id="Ordering-Multiple-Matches">
109 <h4 class="unnumberedsubsec">Ordering Multiple Matches</h4>
110
111 <p>In general there may be more than one way to match a regular expression
112 to an input string. For example, consider the command
113 </p>
114 <div class="example">
115 <pre class="example"> (string-match &quot;(a*)b*&quot; &quot;aabaaabb&quot;)
116 </pre></div>
117
118 <p>Considering only the rules given so far, the value of (list-matches 0 1)
119 might be <code>(&quot;aabb&quot; &quot;aa&quot;)</code> or <code>(&quot;aaab&quot; &quot;aaa&quot;)</code> or <code>(&quot;ab&quot; &quot;a&quot;)</code>
120 or any of several other combinations.
121 To resolve this potential ambiguity <b>string-match</b> chooses among
122 alternatives using the rule <i>first then longest</i>.
123 In other words, it considers the possible matches in order working
124 from left to right across the input string and the pattern, and it
125 attempts to match longer pieces of the input string before shorter
126 ones. More specifically, the following rules apply in decreasing
127 order of priority:
128 </p><ul class="no-bullet">
129 <li> [1]
130 If a regular expression could match two different parts of an input string
131 then it will match the one that begins earliest.
132 </li><li> [2]
133 If a regular expression contains <b>|</b> operators then the leftmost
134 matching sub-expression is chosen.
135 </li><li> [3]
136 In <b>*</b><span class="roman">, </span><b>+</b><span class="roman">, and </span><b>?</b> constructs, longer matches are chosen
137 in preference to shorter ones.
138 </li><li> [4]
139 In sequences of expression components the components are considered
140 from left to right.
141 </li></ul>
142
143 <p>In the example from above, <b>(a*)b*</b><span class="roman"> matches </span><b>aab</b><span class="roman">: the </span><b>(a*)</b>
144 portion of the pattern is matched first and it consumes the leading
145 <b>aa</b><span class="roman">; then the </span><b>b*</b> portion of the pattern consumes the
146 next <b>b</b>. Or, consider the following example:
147 </p>
148 <div class="example">
149 <pre class="example"> (string-match &quot;(ab|a)(b*)c&quot; &quot;xabc&quot;) ==&gt; 1
150 (list-matches 0 1 2 3) ==&gt; (&quot;abc&quot; &quot;ab&quot; &quot;&quot; NIL)
151 (match-beginning 0) ==&gt; 1
152 (match-end 0) ==&gt; 4
153 (match-beginning 1) ==&gt; 1
154 (match-end 1) ==&gt; 3
155 (match-beginning 2) ==&gt; 3
156 (match-end 2) ==&gt; 3
157 (match-beginning 3) ==&gt; -1
158 (match-end 3) ==&gt; -1
159
160 </pre></div>
161
162 <p>In the above example the return value of <code>1</code> (which is <code>&gt; -1</code>)
163 indicates that a match was found. The entire match runs from
164 1 to 4.
165 Rule 4 specifies that <b>(ab|a)</b> gets first shot at the input
166 string and Rule 2 specifies that the <b>ab</b> sub-expression
167 is checked before the <b>a</b> sub-expression.
168 Thus the <b>b</b><span class="roman"> has already been claimed before the </span><b>(b*)</b>
169 component is checked and <b>(b*)</b> must match an empty string.
170 </p>
171 <p>The special characters in the string <code>&quot;\()[]+.*|^$?&quot;</code>,
172 must be quoted, if a simple string search is desired. The function
173 re-quote-string is provided for this purpose.
174 </p><div class="example">
175 <pre class="example">(re-quote-string &quot;*standard*&quot;) ==&gt; &quot;\\*standard\\*&quot;
176
177 (string-match (re-quote-string &quot;*standard*&quot;) &quot;X *standard* &quot;)
178 ==&gt; 2
179
180 (string-match &quot;*standard*&quot; &quot;X *standard* &quot;)
181 Error: Regexp Error: ?+* follows nothing
182 </pre></div>
183 <p>Note there is actually just one <code>\</code> before the <code>*</code>
184 but the printer makes two so that the string can be read, since
185 <code>\</code> is also the lisp quote character. In the last example
186 an error is signalled since the special character <code>*</code> must
187 follow an atom if it is interpreted as a regular expression.
188 </p>
189
190
191
192
193
194
195
•196 </div>
197 </div>
198 <hr>
199 <div class="header">
200 <p>
•201 Previous: <a href="System-Definitions.html">System Definitions</a>, Up: <a href="System-Definitions.html">System Definitions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Function-and-Variable-Index.html" title="Index" rel="index">Index</a>]</p>
202 </div>
203
204
205
206 </body>
207 </html>