📄 manual_regexp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0073)manual_Regexp.html -->
<html>
<head>
<title>MySQL Reference Manual for version 3.23.7-alpha. - H Description of MySQL regular
expression syntax</title>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<!-- This HTML file has been created by texi2html 1.52 (hacked by david@detron.se)
from /dr1/my/masters/mysql-3.23/Docs/manual.texi on 9 December 1999 -->
<meta content="Microsoft FrontPage 3.0" name="GENERATOR">
</head>
<body bgColor="#ffffff" link="#101090" text="#000000" vLink="#7030b0">
<p>Go to the <a href="manual_Introduction.html">first</a>, <a href="manual_Porting.html">previous</a>,
<a href="manual_Unireg.html">next</a>, <a href="manual_Concept_Index.html">last</a>
section, <a href="manual_toc.html">table of contents</a>. </p>
<hr>
<h1><a href="manual_toc.html#Regexp" name="Regexp">H Description of MySQL regular
expression syntax</a></h1>
<p>A regular expression (regex) is a powerful way of specifying a complex search. </p>
<p><strong>MySQL</strong> uses regular Henry Spencer's inplementation of regular
expressions. And that is aimed to conform to POSIX 1003.2. <strong>MySQL</strong> uses the
extended version. </p>
<p>This is a simplistic reference that skips the details. To get more exact information,
see Henry Spencer's <code>regex(7)</code> manual page that is included in the source
distribution. See section <a href="manual_Credits.html#Credits">C Contributors to MySQL</a>.
</p>
<p>A regular expression describes a set of strings. The simplest regexp is one that has no
special characters in it. For example, the regexp <code>hello</code> matches <code>hello</code>
and nothing else. </p>
<p>Nontrivial regular expressions use certain special constructs so that they can match
more than one string. For example, the regexp <code>hello|word</code> matches either the
string <code>hello</code> or the string <code>word</code>. </p>
<p>As a more complex example, the regexp <code>B[an]*s</code> matches any of the strings <code>Bananas</code>,
<code>Baaaaas</code>, <code>Bs</code> and any other string starting with a <code>B</code>,
ending with an <code>s</code>, and containing any number of <code>a</code> or <code>n</code>
characters in between. </p>
<p>A regular expression may use any of the following special characters/constructs:
<dl compact>
<dt><code>^</code> </dt>
<dd>Match the beginning of a string. <pre>mysql> select "fo\nfo" REGEXP "^fo$"; -> 0
mysql> select "fofo" REGEXP "^fo"; -> 1
</pre>
</dd>
<dt><code>$</code> </dt>
<dd>Match the end of a string. <pre>mysql> select "fo\no" REGEXP "^fo\no$"; -> 1
mysql> select "fo\no" REGEXP "^fo$"; -> 0
</pre>
</dd>
<dt><code>.</code> </dt>
<dd>Match any character (including newline). <pre>mysql> select "fofo" REGEXP "^f.*"; -> 1
mysql> select "fo\nfo" REGEXP "^f.*"; -> 1
</pre>
</dd>
<dt><code>a*</code> </dt>
<dd>Match any sequence of zero or more <code>a</code> characters. <pre>mysql> select "Ban" REGEXP "^Ba*n"; -> 1
mysql> select "Baaan" REGEXP "^Ba*n"; -> 1
mysql> select "Bn" REGEXP "^Ba*n"; -> 1
</pre>
</dd>
<dt><code>a+</code> </dt>
<dd>Match any sequence of one or more <code>a</code> characters. <pre>mysql> select "Ban" REGEXP "^Ba+n"; -> 1
mysql> select "Bn" REGEXP "^Ba+n"; -> 0
</pre>
</dd>
<dt><code>a?</code> </dt>
<dd>Match either zero or one <code>a</code> character. <pre>mysql> select "Bn" REGEXP "^Ba?n"; -> 1
mysql> select "Ban" REGEXP "^Ba?n"; -> 1
mysql> select "Baan" REGEXP "^Ba?n"; -> 0
</pre>
</dd>
<dt><code>de|abc</code> </dt>
<dd>Match either of the sequences <code>de</code> or <code>abc</code>. <pre>mysql> select "pi" REGEXP "pi|apa"; -> 1
mysql> select "axe" REGEXP "pi|apa"; -> 0
mysql> select "apa" REGEXP "pi|apa"; -> 1
mysql> select "apa" REGEXP "^(pi|apa)$"; -> 1
mysql> select "pi" REGEXP "^(pi|apa)$"; -> 1
mysql> select "pix" REGEXP "^(pi|apa)$"; -> 0
</pre>
</dd>
<dt><code>(abc)*</code> </dt>
<dd>Match zero or more instances of the sequence <code>abc</code>. <pre>mysql> select "pi" REGEXP "^(pi)*$"; -> 1
mysql> select "pip" REGEXP "^(pi)*$"; -> 0
mysql> select "pipi" REGEXP "^(pi)*$"; -> 1
</pre>
</dd>
<dt><code>{1}</code> </dt>
<dd> </dd>
<dt><code>{2,3}</code> </dt>
<dd>The is a more general way of writing regexps that match many occurrences of the previous
atom. <dl compact>
<dt><code>a*</code> </dt>
<dd>Can be written as <code>a{0,}</code>. </dd>
<dt><code>a+</code> </dt>
<dd>Can be written as <code>a{1,}</code>. </dd>
<dt><code>a?</code> </dt>
<dd>Can be written as <code>a{0,1}</code>. </dd>
</dl>
<p>To be more precise, an atom followed by a bound containing one integer <code>i</code>
and no comma matches a sequence of exactly <code>i</code> matches of the atom. An atom
followed by a bound containing one integer <code>i</code> and a comma matches a sequence
of <code>i</code> or more matches of the atom. An atom followed by a bound containing two
integers <code>i</code> and <code>j</code> matches a sequence of <code>i</code> through <code>j</code>
(inclusive) matches of the atom. Both arguments must <code>0 >= value <= RE_DUP_MAX
(default 255)</code>. If there are two arguments, the second must be greater than or equal
to the first. </p>
</dd>
<dt><code>[a-dX]</code> </dt>
<dd> </dd>
<dt><code>[^a-dX]</code> </dt>
<dd>Matches any character which is (or is not, if ^ is used) either <code>a</code>, <code>b</code>,
<code>c</code>, <code>d</code> or <code>X</code>. To include a literal <code>]</code>
character, it must immediately follow the opening bracket <code>[</code>. To include a
literal <code>-</code> character, it must be written first or last. So <code>[0-9]</code>
matches any decimal digit. Any character that does not have a defined meaning inside a <code>[]</code>
pair has no special meaning and matches only itself. <pre>mysql> select "aXbc" REGEXP "[a-dXYZ]"; -> 1
mysql> select "aXbc" REGEXP "^[a-dXYZ]$"; -> 0
mysql> select "aXbc" REGEXP "^[a-dXYZ]+$"; -> 1
mysql> select "aXbc" REGEXP "^[^a-dXYZ]+$"; -> 0
mysql> select "gheis" REGEXP "^[^a-dXYZ]+$"; -> 1
mysql> select "gheisa" REGEXP "^[^a-dXYZ]+$"; -> 0
</pre>
</dd>
<dt><code>[[.characters.]]</code> </dt>
<dd>The sequence of characters of that collating element. The sequence is a single element
of the bracket expression's list. A bracket expression containing a multi-character
collating element can thus match more than one character, e.g., if the collating sequence
includes a <code>ch</code> collating element, then the regular expression <code>[[.ch.]]*c</code>
matches the first five characters of <code>chchcc</code>. </dd>
<dt><code>[=character_class=]</code> </dt>
<dd>An equivalence class, standing for the sequences of characters of all collating elements
equivalent to that one, including itself. For example, if <code>o</code> and <code>(+)</code>
are the members of an equivalence class, then <code>[[=o=]]</code>, <code>[[=(+)=]]</code>,
and <code>[o(+)]</code> are all synonymous. An equivalence class may not be an endpoint of
a range. </dd>
<dt><code>[:character_class:]</code> </dt>
<dd>Within a bracket expression, the name of a character class enclosed in <code>[:</code>
and <code>:]</code> stands for the list of all characters belonging to that class.
Standard character class names are: <table border="1" width="100%" NOSAVE>
<TBODY>
<tr>
<td>alnum </td>
<td>digit </td>
<td>punct </td>
</tr>
<tr>
<td>alpha </td>
<td>graph </td>
<td>space </td>
</tr>
<tr>
<td>blank </td>
<td>lower </td>
<td>upper </td>
</tr>
<tr>
<td>cntrl </td>
<td>print </td>
<td>xdigit </td>
</tr>
</TBODY>
</table>
<p>These stand for the character classes defined in the <code>ctype(3)</code> manual page.
A locale may provide others. A character class may not be used as an endpoint of a range. </p>
<pre>mysql> select "justalnums" REGEXP "[[:alnum:]]+"; -> 1
mysql> select "!!" REGEXP "[[:alnum:]]+"; -> 0
</pre>
</dd>
<dt><code>[[:<:]]</code> </dt>
<dd> </dd>
<dt><code>[[:>:]]</code> </dt>
<dd>These match the null string at the beginning and end of a word respectively. A word is
defined as a sequence of word characters which is neither preceded nor followed by word
characters. A word character is an alnum character (as defined by <code>ctype(3)</code>)
or an underscore (<code>_</code>). <pre>mysql> select "a word a" REGEXP "[[:<:]]word[[:>:]]"; -> 1
mysql> select "a xword a" REGEXP "[[:<:]]word[[:>:]]"; -> 0
</pre>
</dd>
</dl>
<pre>mysql> select "weeknights" REGEXP "^(wee|week)(knights|nights)$"; -> 1
</pre>
<hr>
<p>Go to the <a href="manual_Introduction.html">first</a>, <a href="manual_Porting.html">previous</a>,
<a href="manual_Unireg.html">next</a>, <a href="manual_Concept_Index.html">last</a>
section, <a href="manual_toc.html">table of contents</a>. </p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -