Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4997

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>regular expressions</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_match_lp__String_cm__String_rp.html">next</a> | <a href="_separate.html">previous</a> | <a href="_horizontal__Join.html">forward</a> | <a href="_separate.html">backward</a> | <a href="_strings_spand_spnets.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_strings_spand_spnets.html" title="">strings and nets</a> > <a href="_regular_spexpressions.html" title="">regular expressions</a></div>
<hr/>
<div><h1>regular expressions</h1>
<div><p>A regular expression is a string that specifies a pattern that describes a set of matching subject strings.  Regular expressions are constructed inductively as follows.  Ordinary (non-special) characters match themselves.  A concatenation of regular expressions matches the concatenation of corresponding matching subject strings.  Regular expressions separated by the character <tt>|</tt> match strings matched by any.  Parentheses can be used for grouping, and results about which substrings of the target string matched which parenthesized subexpression of the regular expression can be returned.</p>
<p>The special characters are those appearing in the following constructions.  The special character <tt>\</tt> may be confusing, as inside a string delimited by quotation marks (<tt>"..."</tt>), you type two of them to get one, whereas inside a string delimited by triple slashes (<tt>///...///</tt>), you type one to get one.  Thus regular expressions delimited by triple slashes are more readable.</p>
<ul><li><tt>.</tt> -- match any character except newline</li>
<li><tt>^</tt> -- match the beginning of the string or the beginning of a line</li>
<li><tt>$</tt> -- match the end of the string or the end of a line</li>
<li><tt>*</tt> -- match previous expression 0 or more times</li>
<li><tt>+</tt> -- match previous expression 1 or more times</li>
<li><tt>?</tt> -- match previous expression 1 or 0 times</li>
<li><tt>(...)</tt> -- subpattern grouping</li>
<li><tt>|</tt> -- match expression to left or expression to right</li>
<li><tt>{m,n}</tt> -- match previous expression at least m and at most n times</li>
<li><tt>{,n}</tt> -- match previous expression at most n times</li>
<li><tt>{m,}</tt> -- match previous expression at least m times</li>
<li><tt>\i</tt> -- match the same string that the i-th parenthesized subpattern matched</li>
<li><tt>[...]</tt> -- match listed characters, ranges, or classes</li>
<li><tt>[^...]</tt> -- match non-listed characters, ranges, or classes</li>
<li><tt>\b</tt> -- match word boundary</li>
<li><tt>\B</tt> -- match within word</li>
<li><tt>\&lt;</tt> -- match beginning of word</li>
<li><tt>\></tt> -- match end of word</li>
<li><tt>\w</tt> -- match word-constituent character</li>
<li><tt>\W</tt> -- match non-word-constituent character</li>
<li><tt>\`</tt> -- match beginning of string</li>
<li><tt>\'</tt> -- match end of string</li>
</ul>
There are the following character classes.<ul><li><tt>[:alnum:]</tt> -- letters and digits</li>
<li><tt>[:alpha:]</tt> -- letters</li>
<li><tt>[:blank:]</tt> -- a space or tab</li>
<li><tt>[:cntrl:]</tt> -- control characters</li>
<li><tt>[:digit:]</tt> -- digits</li>
<li><tt>[:graph:]</tt> -- same as [:print:] except omits space</li>
<li><tt>[:lower:]</tt> -- lowercase letters</li>
<li><tt>[:print:]</tt> -- printable characters</li>
<li><tt>[:punct:]</tt> -- neither control nor alphanumeric characters</li>
<li><tt>[:space:]</tt> -- space, tab, carriage return, newline, vertical tab, and form feed</li>
<li><tt>[:upper:]</tt> -- uppercase letters</li>
<li><tt>[:xdigit:]</tt> -- hexadecimal digits</li>
</ul>
In order to match one of the special characters itself, precede it with a backslash.<p>We illustrate the use of regular expressions with <a href="_regex.html" title="">regex(String,String)</a>.</p>
<table class="examples"><tr><td><pre>i1 : s = "1abcddddeF2";</pre>
</td></tr>
<tr><td><pre>i2 : regex("d", s)

o2 = {(4, 1)}

o2 : List</pre>
</td></tr>
<tr><td><pre>i3 : substring(oo#0,s)

o3 = d</pre>
</td></tr>
<tr><td><pre>i4 : regex("d*", s)

o4 = {(0, 0)}

o4 : List</pre>
</td></tr>
<tr><td><pre>i5 : substring(oo#0,s)

o5 = </pre>
</td></tr>
<tr><td><pre>i6 : regex("d+", s)

o6 = {(4, 4)}

o6 : List</pre>
</td></tr>
<tr><td><pre>i7 : substring(oo#0,s)

o7 = dddd</pre>
</td></tr>
<tr><td><pre>i8 : regex("d+", "1abceF2")</pre>
</td></tr>
<tr><td><pre>i9 : regex("cdd+e", s)

o9 = {(3, 6)}

o9 : List</pre>
</td></tr>
<tr><td><pre>i10 : substring(oo#0,s)

o10 = cdddde</pre>
</td></tr>
<tr><td><pre>i11 : regex("cd(d+)e", s)

o11 = {(3, 6), (5, 3)}

o11 : List</pre>
</td></tr>
<tr><td><pre>i12 : substring(oo#0,s),substring(oo#1,s)

o12 = (cdddde, ddd)

o12 : Sequence</pre>
</td></tr>
<tr><td><pre>i13 : regex("[a-z]+", s)

o13 = {(1, 8)}

o13 : List</pre>
</td></tr>
<tr><td><pre>i14 : substring(oo#0,s)

o14 = abcdddde</pre>
</td></tr>
<tr><td><pre>i15 : t = "Dog cat cat.";</pre>
</td></tr>
<tr><td><pre>i16 : regex("[[:alpha:]]+", t)

o16 = {(0, 3)}

o16 : List</pre>
</td></tr>
<tr><td><pre>i17 : regex("([[:alpha:]]+) *\\1",t)

o17 = {(4, 7), (4, 3)}

o17 : List</pre>
</td></tr>
<tr><td><pre>i18 : substring(oo#0,t),substring(oo#1,t)

o18 = (cat cat, cat)

o18 : Sequence</pre>
</td></tr>
</table>
For complete documentation on regular expressions see the entry for <tt>regex</tt> in section 7 of the unix man pages, or read the the GNU <tt>regex</tt> manual.</div>
<div class="single"><h2>Caveat</h2>
<div>Regular expression matching is done by calls to a C library, where the convention is that the end of a string is signalled by a byte containing zero, whereas in Macaulbay 2, strings may contain bytes containing zero.  Hence regular expression parsing and matching ignore any bytes containing zero, as well as any subsequent bytes, potentially yielding surprising results.</div>
</div>
<div><h3>Menu</h3>
<h4>functions that accept regular expressions</h4>
<ul><li><span>match, see <span><a href="_match_lp__String_cm__String_rp.html" title="regular expression matching">match(String,String)</a> -- regular expression matching</span></span></li>
<li><span><a href="_regex.html" title="">regex</a></span></li>
<li><span><a href="_replace.html" title="replacement in strings and lists">replace</a> -- replacement in strings and lists</span></li>
<li><span><a href="_select_lp__String_cm__String_rp.html" title="select substrings matching a regular expression from a string">select(String,String)</a> -- select substrings matching a regular expression from a string</span></li>
<li><span><a href="_select_lp__String_cm__String_cm__String_rp.html" title="select substrings matching a regular expression from a string">select(String,String,String)</a> -- select substrings matching a regular expression from a string</span></li>
</ul>
</div>
</div>
</body>
</html>