Mysql Regular Expression query and instance

Source: Internet
Author: User
Tags mysql tutorial

Mysql tutorial Regular Expression query and instance
 
^ Match the start part of the string
$ Match the end part of a string
$ SQL = "SELECT 'fonfo 'regexp '^ fo $' from string_find ";
 
. Match any character (including carriage return and new line)
$ SQL = "SELECT 'fofofo' REGEXP '^ f. * $' from string_find ";
 
A * matches any sequence of 0 or multiple a characters
 
$ SQL = "SELECT 'Ban 'regexp' ^ Ba * n' from string_find ";
 
A + matches any sequence of one or more a characters
 
$ SQL = "SELECT 'Ban 'regexp' ^ Ba + N' from string_find ";
 
 
A? Match 0 or 1 a character
 
$ SQL = "SELECT 'bn 'regexp '^ Ba? N' from string_find ";

De | abc matching sequence de or abc
(Abc) * matches 0 or multiple instances of the sequence abc.
The {1}, {2, 3} {n} or {m, n} symbol provides a more common way to write regular expressions, many of the aforementioned atoms (or "parts") that can match the pattern "). Both m and n are integers.
O *
Can be written as a {0 ,}.
 
O a +
 
Can be written as a {1 ,}.
 
O?
 
Can be written as a {0, 1 }.
 
More accurately, a {n} exactly matches n instances of. A {n,} matches n or more instances of. A {m, n} matches m ~ of ~ N instances, including m and n.
 
M and n must be between 0 and ~ The range of RE_DUP_MAX (255 by default) includes 0 and RE_DUP_MAX. If both m and n are given, m must be less than or equal
[A-dX], [^ a-dX] matches any character that is (or is not, if ^ is used) a, B, c, d, or X. The "-" character between two other characters constitutes a range, and matches all characters starting from 1st characters to 2nd characters. For example, [0-9] matches any decimal number. To include the text character "]", it must be followed by the brackets. To contain the text character "-", it must be written first or last. For any character that does not define any special meaning for [], it only matches itself.
[. Characters.] In the brackets expression (using [and]), match the character sequence used for checking the element. The character is a single character or a new line or another character name. In the regexp/cname. h file, you can find the complete list of character names.
 
First, log in to a database tutorial cc,
 
Create database cc;
 
Create a table
 
Create table if not exists 'string _ find '(
'Id' int (4) not null auto_increment,
'Charlist' varchar (100) default NULL,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 7;
 
Import Test Data
 
Insert into 'string _ find '('id', 'charlist') VALUES
(1, 'Pioneer Deng '),
(2, 'Pioneer Deng '),
(5, 'fdafdsaf '),
(6, 'www. bKjia. c0m ');
 
Now everything is ready.
 
*/

$ DbHost = 'localhost'; // It can be unchanged.
$ DbUser = 'root'; // your mysql user name
$ DbPass = 'root'; // change it to your 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 record found ');
}

/*
The results are different, but the mysql regular expression is used.

This article was originally published on the first stop of www. bKjia. c0m Chinese WEB.

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.