MySQL tutorial regular expression queries and instances
^ Start part of matching string
$ match End of string
$sql = "Select ' Fonfo ' REGEXP ' ^fo$ ' from String_find";
. Match any character (including carriage return and new line)
$sql = "Select ' Fofo ' REGEXP ' ^f.*$ ' from String_find";
A * matches any sequence of 0 or more a characters
$sql = "Select ' Ban ' REGEXP ' ^ba*n ' from String_find";
A + matches any sequence of 1 or more a characters
$sql = "Select ' Ban ' REGEXP ' ^ba+n ' from String_find";
A? Match 0 or 1 a characters
$sql = "Select ' Bn ' REGEXP ' ^ba?n ' from String_find";
DE|ABC matching sequence de or ABC
(ABC) * matches 0 or more instances of the sequence ABC.
{1}, {2,3} {n} or {m,n} notation provides a more general way to write regular expressions that can match many of the aforementioned atoms (or "parts") of a pattern. M and n are all integers.
O *
Can be written as a{0,}.
O +
Can be written as A{1,}.
o A?
Can be written to a{0,1}.
More accurately, a{n} matches exactly the n instances of a. A{n,} matches N or more instances of a. A{m,n} matches a m~n instance of a, containing M and N.
M and n must be in the range of 0~re_dup_max (default 255), containing 0 and Re_dup_max. If both the M and n,m must be less than or equal to
[A-DX], [^A-DX] matches any character that is (or is not, if used ^) A, B, C, D, or X. The "-" character between two other characters constitutes a range that matches all characters from the beginning of the 1th character to the 2nd character. For example, [0-9] matches any decimal digit. To include the literal character "]", it must be immediately after the opening parenthesis "[". To include the literal character "-", it must be written first or last. for [] Any character that does not have any special meaning defined internally, only matches itself.
[. Characters.] In parentheses expressions (using [and]), match the sequence of characters used for proofing elements. A character is a single character or a character name such as a new line. In file regexp/cname.h, you can find a complete list of character names.
First log in to a database tutorial cc,
Create DATABASE cc;
Create a table again
CREATE TABLE IF not EXISTS ' String_find ' (
' ID ' int (4) not NULL auto_increment,
' charList ' varchar default NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8 auto_increment=7;
Import test Data
INSERT into ' string_find ' (' id ', ' charList ') VALUES
(1, ' Deng Pioneer '),
(2, ' Deng Pioneer '),
(5, ' Fdafdsaf '),
(6, ' www.111cn.net ');
All right, everything's up.
*/
$dbHost = ' localhost '; Can not change
$dbUser = ' root '; You mysql user name
$dbPass = ' root '; Change to your own MySQL server password
$db = ' CC ';
$charset = ' GBK ';
$conn = Mysql_pconnect ($dbHost, $dbUser, $dbPass) or Die (Mysql_error ());
mysql_select_db ($db, $conn);
mysql_query ("Set Names ' $charset ')";
$sql = ' Select * from String_find where charList REGEXP ' ^ Deng ';
$query = mysql_query ($sql);
if (mysql_num_rows ($query))
{
while ($rs = Mysql_fetch_array ($query))
{
echo $rs [1], ' <br/> ';
}
}
Else
{
Exit (' No Related records found ');
}
/*
The result is not the same, just using the MySQL regular expression of
This article is original in Www.111cn.net China Web first station, reprinted annotated Source