/usr/share/doc/ne/html/Regular-Expressions.html is in ne-doc 2.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | <html lang="en">
<head>
<title>Regular Expressions - ne's manual</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="ne's manual">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Reference.html#Reference" title="Reference">
<link rel="prev" href="Menus.html#Menus" title="Menus">
<link rel="next" href="Automatic-Preferences.html#Automatic-Preferences" title="Automatic Preferences">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Regular-Expressions"></a>
<p>
Next: <a rel="next" accesskey="n" href="Automatic-Preferences.html#Automatic-Preferences">Automatic Preferences</a>,
Previous: <a rel="previous" accesskey="p" href="Menus.html#Menus">Menus</a>,
Up: <a rel="up" accesskey="u" href="Reference.html#Reference">Reference</a>
<hr>
</div>
<h3 class="section">3.8 Regular Expressions</h3>
<p><a name="index-Regular-Expressions-73"></a>
Regular expressions are a powerful way of specifying complex search and
replace operations. <code>ne</code> supports the full regular expression
syntax on US-ASCII and 8-bit buffers, but has to impose a restriction on
character sets when searching in UTF-8 text. See <a href="UTF_002d8-Support.html#UTF_002d8-Support">UTF-8 Support</a>.
<h4 class="subsection">3.8.1 Syntax</h4>
<p>The following section is taken (with minor modifications) from the GNU regular
expression library documentation and is Copyright © Free Software
Foundation.
<p>A regular expression describes a set of strings. The simplest case is one
that describes a particular string; for example, the string ‘<samp><span class="samp">foo</span></samp>’ when
regarded as a regular expression matches ‘<samp><span class="samp">foo</span></samp>’ and nothing else.
Nontrivial regular expressions use certain special constructs so that they
can match more than one string. For example, the regular expression
‘<samp><span class="samp">foo|bar</span></samp>’ matches either the string ‘<samp><span class="samp">foo</span></samp>’ or the string
‘<samp><span class="samp">bar</span></samp>’; the regular expression ‘<samp><span class="samp">c[ad]*r</span></samp>’ matches any of the strings
‘<samp><span class="samp">cr</span></samp>’, ‘<samp><span class="samp">car</span></samp>’, ‘<samp><span class="samp">cdr</span></samp>’, ‘<samp><span class="samp">caar</span></samp>’, ‘<samp><span class="samp">cadddar</span></samp>’ and all other
such strings with any number of ‘<samp><span class="samp">a</span></samp>’'s and ‘<samp><span class="samp">d</span></samp>’'s.
<p>Regular expressions have a syntax in which a few characters are special
constructs and the rest are <dfn>ordinary</dfn>. An ordinary character is a
simple regular expression which matches that character and nothing else. The
special characters are ‘<samp><span class="samp">$</span></samp>’, ‘<samp><span class="samp">^</span></samp>’, ‘<samp><span class="samp">.</span></samp>’, ‘<samp><span class="samp">*</span></samp>’, ‘<samp><span class="samp">+</span></samp>’,
‘<samp><span class="samp">?</span></samp>’, ‘<samp><span class="samp">[</span></samp>’, ‘<samp><span class="samp">]</span></samp>’ , ‘<samp><span class="samp">(</span></samp>’, ‘<samp><span class="samp">)</span></samp>’ and ‘<samp><span class="samp">\</span></samp>’. Any other
character appearing in a regular expression is ordinary, unless a ‘<samp><span class="samp">\</span></samp>’
precedes it.
<p>For example, ‘<samp><span class="samp">f</span></samp>’ is not a special character, so it is ordinary,
and therefore ‘<samp><span class="samp">f</span></samp>’ is a regular expression that matches the string ‘<samp><span class="samp">f</span></samp>’
and no other string. (It does <em>not</em> match the string ‘<samp><span class="samp">ff</span></samp>’.) Likewise,
‘<samp><span class="samp">o</span></samp>’ is a regular expression that matches only ‘<samp><span class="samp">o</span></samp>’.
<p>Any two regular expressions <var>a</var> and <var>b</var> can be concatenated.
The result is a regular expression that matches a string if <var>a</var>
matches some amount of the beginning of that string and <var>b</var>
matches the rest of the string.
<p>As a simple example, we can concatenate the regular expressions
‘<samp><span class="samp">f</span></samp>’ and ‘<samp><span class="samp">o</span></samp>’ to get the regular expression ‘<samp><span class="samp">fo</span></samp>’,
which matches only the string ‘<samp><span class="samp">fo</span></samp>’. Still trivial.
<p>Note: special characters are treated as ordinary ones if they are in
contexts where their special meanings make no sense. For example,
‘<samp><span class="samp">*foo</span></samp>’ treats ‘<samp><span class="samp">*</span></samp>’ as ordinary since there is no preceding
expression on which the ‘<samp><span class="samp">*</span></samp>’ can act. It is poor practice to depend on
this behaviour; better to quote the special character anyway, regardless of
where is appears.
<p>The following are the characters and character sequences that have special
meaning within regular expressions. Any character not mentioned here is not
special; it stands for exactly itself for the purposes of searching and
matching.
<dl>
<dt>‘<samp><span class="samp">.</span></samp>’<dd>is a special character that matches anything except a newline. Using
concatenation, we can make regular expressions like ‘<samp><span class="samp">a.b</span></samp>’, which matches
any three-character string which begins with ‘<samp><span class="samp">a</span></samp>’ and ends with
‘<samp><span class="samp">b</span></samp>’.
<br><dt>‘<samp><span class="samp">*</span></samp>’<dd>is not a construct by itself; it is a suffix, which means the preceding
regular expression is to be repeated as many times as possible. In
‘<samp><span class="samp">fo*</span></samp>’, the ‘<samp><span class="samp">*</span></samp>’ applies to the ‘<samp><span class="samp">o</span></samp>’, so ‘<samp><span class="samp">fo*</span></samp>’ matches
‘<samp><span class="samp">f</span></samp>’ followed by any number of ‘<samp><span class="samp">o</span></samp>’'s.
<p>The case of zero ‘<samp><span class="samp">o</span></samp>’'s is allowed: ‘<samp><span class="samp">fo*</span></samp>’ does match
‘<samp><span class="samp">f</span></samp>’.
<p>‘<samp><span class="samp">*</span></samp>’ always applies to the <em>smallest</em> possible preceding
expression. Thus, ‘<samp><span class="samp">fo*</span></samp>’ has a repeating ‘<samp><span class="samp">o</span></samp>’, not a repeating
‘<samp><span class="samp">fo</span></samp>’.
<br><dt>‘<samp><span class="samp">+</span></samp>’<dd>‘<samp><span class="samp">+</span></samp>’ is like ‘<samp><span class="samp">*</span></samp>’ except that at least one match for the preceding
pattern is required for ‘<samp><span class="samp">+</span></samp>’. Thus, ‘<samp><span class="samp">c[ad]+r</span></samp>’ does not match
‘<samp><span class="samp">cr</span></samp>’ but does match anything else that ‘<samp><span class="samp">c[ad]*r</span></samp>’ would match.
<br><dt>‘<samp><span class="samp">?</span></samp>’<dd>‘<samp><span class="samp">?</span></samp>’ is like ‘<samp><span class="samp">*</span></samp>’ except that it allows either zero or one match for
the preceding pattern. Thus, ‘<samp><span class="samp">c[ad]?r</span></samp>’ matches ‘<samp><span class="samp">cr</span></samp>’ or ‘<samp><span class="samp">car</span></samp>’
or ‘<samp><span class="samp">cdr</span></samp>’, and nothing else.
<br><dt>‘<samp><span class="samp">[ ... ]</span></samp>’<dd>‘<samp><span class="samp">[</span></samp>’ begins a <dfn>character set</dfn>, which is terminated by a ‘<samp><span class="samp">]</span></samp>’.
In the simplest case, the characters between the two form the set.
Thus, ‘<samp><span class="samp">[ad]</span></samp>’ matches either ‘<samp><span class="samp">a</span></samp>’ or ‘<samp><span class="samp">d</span></samp>’,
and ‘<samp><span class="samp">[ad]*</span></samp>’ matches any string of ‘<samp><span class="samp">a</span></samp>’'s and ‘<samp><span class="samp">d</span></samp>’'s
(including the empty string), from which it follows that
‘<samp><span class="samp">c[ad]*r</span></samp>’ matches ‘<samp><span class="samp">car</span></samp>’, <i>et cetera</i>.
<p>Character ranges can also be included in a character set, by writing two
characters with a ‘<samp><span class="samp">-</span></samp>’ between them. Thus, ‘<samp><span class="samp">[a-z]</span></samp>’ matches any
lower-case letter. Ranges may be intermixed freely with individual
characters, as in ‘<samp><span class="samp">[a-z$%.]</span></samp>’, which matches any lower case letter or
‘<samp><span class="samp">$</span></samp>’, ‘<samp><span class="samp">%</span></samp>’ or period.
<p>Note that the usual special characters are not special any more inside a
character set. A completely different set of special characters exists
inside character sets: ‘<samp><span class="samp">]</span></samp>’, ‘<samp><span class="samp">-</span></samp>’ and ‘<samp><span class="samp">^</span></samp>’.
<p>To include a ‘<samp><span class="samp">]</span></samp>’ in a character set, you must make it
the first character. For example, ‘<samp><span class="samp">[]a]</span></samp>’ matches ‘<samp><span class="samp">]</span></samp>’ or ‘<samp><span class="samp">a</span></samp>’.
To include a ‘<samp><span class="samp">-</span></samp>’, you must use it in a context where it cannot possibly
indicate a range: that is, as the first character, or immediately
after a range.
<p>Note that when searching in UTF-8 text, a character set may contain
US-ASCII characters only.
<br><dt>‘<samp><span class="samp">[^ ... ]</span></samp>’<dd>‘<samp><span class="samp">[^</span></samp>’ begins a <dfn>complement character set</dfn>, which matches any
character except the ones specified. Thus, ‘<samp><span class="samp">[^a-z0-9A-Z]</span></samp>’ matches
all characters <em>except</em> letters and digits. Also in this case, when
searching in UTF-8 text a complemented character set may contain US-ASCII
characters only.
<p>‘<samp><span class="samp">^</span></samp>’ is not special in a character set unless it is the first character.
The character following the ‘<samp><span class="samp">^</span></samp>’ is treated as if it were first (it may
be a ‘<samp><span class="samp">-</span></samp>’ or a ‘<samp><span class="samp">]</span></samp>’).
<br><dt>‘<samp><span class="samp">^</span></samp>’<dd>is a special character that matches the empty string – but only if at the
beginning of a line in the text being matched. Otherwise it fails to match
anything. Thus, ‘<samp><span class="samp">^foo</span></samp>’ matches a ‘<samp><span class="samp">foo</span></samp>’ that occurs at the
beginning of a line.
<br><dt>‘<samp><span class="samp">$</span></samp>’<dd>is similar to ‘<samp><span class="samp">^</span></samp>’ but matches only at the end of a line. Thus,
‘<samp><span class="samp">xx*$</span></samp>’ matches a string of one or more ‘<samp><span class="samp">x</span></samp>’'s at the end of a
line.
<br><dt>‘<samp><span class="samp">\</span></samp>’<dd>has two functions: it quotes the above special characters (including
‘<samp><span class="samp">\</span></samp>’), and it introduces additional special constructs.
<p>Because ‘<samp><span class="samp">\</span></samp>’ quotes special characters, ‘<samp><span class="samp">\$</span></samp>’ is a regular
expression that matches only ‘<samp><span class="samp">$</span></samp>’, and ‘<samp><span class="samp">\[</span></samp>’ is a regular
expression that matches only ‘<samp><span class="samp">[</span></samp>’, and so on.
<p>For the most part, ‘<samp><span class="samp">\</span></samp>’ followed by any character matches only that
character. However, there are several exceptions: characters which, when
preceded by ‘<samp><span class="samp">\</span></samp>’, are special constructs. Such characters are always
ordinary when encountered on their own.
<br><dt>‘<samp><span class="samp">|</span></samp>’<dd>specifies an alternative. Two regular expressions <var>a</var> and <var>b</var> with
‘<samp><span class="samp">|</span></samp>’ in between form an expression that matches anything that either
<var>a</var> or <var>b</var> will match.
<p>Thus, ‘<samp><span class="samp">foo|bar</span></samp>’ matches either ‘<samp><span class="samp">foo</span></samp>’ or ‘<samp><span class="samp">bar</span></samp>’ but no other
string.
<p>‘<samp><span class="samp">|</span></samp>’ applies to the largest possible surrounding expressions. Only a
surrounding ‘<samp><span class="samp">( ... )</span></samp>’ grouping can limit the grouping power of
‘<samp><span class="samp">|</span></samp>’.
<br><dt>‘<samp><span class="samp">( ... )</span></samp>’<dd>is a grouping construct that serves three purposes:
<ol type=1 start=1>
<li>To enclose a set of ‘<samp><span class="samp">|</span></samp>’ alternatives for other operations.
Thus, ‘<samp><span class="samp">(foo|bar)x</span></samp>’ matches either ‘<samp><span class="samp">foox</span></samp>’ or ‘<samp><span class="samp">barx</span></samp>’.
<li>To enclose a complicated expression for the postfix ‘<samp><span class="samp">*</span></samp>’ to operate on.
Thus, ‘<samp><span class="samp">ba(na)*</span></samp>’ matches ‘<samp><span class="samp">bananana</span></samp>’ <i>et cetera</i>, with any (zero or
more) number of ‘<samp><span class="samp">na</span></samp>’'s.
<li>To mark a matched substring for future reference.
</ol>
<p>This last application is not a consequence of the idea of a parenthetical
grouping; it is a separate feature that happens to be assigned as a second
meaning to the same ‘<samp><span class="samp">( ... )</span></samp>’ construct because there is no
conflict in practice between the two meanings. Here is an explanation of
this feature:
<br><dt>‘<samp><span class="samp">\</span><var>digit</var></samp>’<dd>After the end of a ‘<samp><span class="samp">( ... )</span></samp>’ construct, the matcher remembers the
beginning and end of the text matched by that construct. Then, later on in
the regular expression, you can use ‘<samp><span class="samp">\</span></samp>’ followed by <var>digit</var> to mean
“match the same text matched the <var>digit</var>'th time by the ‘<samp><span class="samp">(
... )</span></samp>’ construct.” The ‘<samp><span class="samp">( ... )</span></samp>’ constructs are numbered
in order of commencement in the regexp.
<p>The strings matching the first nine ‘<samp><span class="samp">( ... )</span></samp>’ constructs appearing
in a regular expression are assigned numbers 1 through 9 in order of their
beginnings.
‘<samp><span class="samp">\1</span></samp>’ through ‘<samp><span class="samp">\9</span></samp>’ may be used to refer to the text matched by
the corresponding ‘<samp><span class="samp">( ... )</span></samp>’ construct.
<p>For example, ‘<samp><span class="samp">(.+)\1</span></samp>’ matches any non empty string that is composed of
two identical halves. The ‘<samp><span class="samp">(.+)</span></samp>’ matches the first half, which may be
anything non empty, but the ‘<samp><span class="samp">\1</span></samp>’ that follows must match the same exact
text.
<br><dt>‘<samp><span class="samp">\b</span></samp>’<dd>matches the empty string, but only if it is at the beginning or
end of a word. Thus, ‘<samp><span class="samp">\bfoo\b</span></samp>’ matches any occurrence of
‘<samp><span class="samp">foo</span></samp>’ as a separate word. ‘<samp><span class="samp">\bball(s|)\b</span></samp>’ matches
‘<samp><span class="samp">ball</span></samp>’ or ‘<samp><span class="samp">balls</span></samp>’ as a separate word.
<br><dt>‘<samp><span class="samp">\B</span></samp>’<dd>matches the empty string, provided it is <em>not</em> at the beginning or end
of a word.
<br><dt>‘<samp><span class="samp">\<</span></samp>’<dd>matches the empty string, but only if it is at the beginning
of a word.
<br><dt>‘<samp><span class="samp">\></span></samp>’<dd>matches the empty string, but only if it is at the end of a word.
<br><dt>‘<samp><span class="samp">\w</span></samp>’<dd>matches any word-constituent character. These are US-ASCII letters,
numbers and the underscore, independently on the buffer encoding.
<br><dt>‘<samp><span class="samp">\W</span></samp>’<dd>matches any character that is not a word-constituent.
</dl>
<h4 class="subsection">3.8.2 Replacing regular expressions</h4>
<p>Also the replacement string has some special feature when doing a regular
expression search and replace. Exactly as during the search, ‘<samp><span class="samp">\</span></samp>’ followed
by <var>digit</var> stands for “the text matched the <var>digit</var>'th time by the
‘<samp><span class="samp">( ... )</span></samp>’ construct in the search expression”. Moreover, ‘<samp><span class="samp">\0</span></samp>’
represent the whole string matched by the regular expression. Thus, for
instance, the replace string ‘<samp><span class="samp">\0\0</span></samp>’ has the effect of doubling any string
matched.
<p>Another example: if you search for ‘<samp><span class="samp">(a+)(b+)</span></samp>’, replacing with
‘<samp><span class="samp">\2x\1</span></samp>’, you will match any string composed by a series of ‘<samp><span class="samp">a</span></samp>’'s
followed by a series of ‘<samp><span class="samp">b</span></samp>’'s, and you will replace it with the
string obtained by moving the ‘<samp><span class="samp">a</span></samp>’ in front of the ‘<samp><span class="samp">b</span></samp>’'s, adding
moreover ‘<samp><span class="samp">x</span></samp>’ inbetween. For instance, ‘<samp><span class="samp">aaaab</span></samp>’ will be matched and
replaced by ‘<samp><span class="samp">bxaaaa</span></samp>’.
<p>Note that the backslash character can escape itself. Thus, to put a
backslash in the replacement string, you have to use ‘<samp><span class="samp">\\</span></samp>’.
</body></html>
|