MySQL Regular expression Query and instance

Source: Internet
Author: User
Tags mysql tutorial create database

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.