Advanced search for data using regular expressions [regexp]

Regular Expressions Introduction

Regular expressions are a special set of characters used to match text, if you want to retrieve a phone number from a text just use regular expressions, all the files if you need to find names that contain numbers can use regular, if you you want in the text block you will find all repeated words, you can use regular.

Can be regular [regexp], understood as a direct [to match]

The basic character matches

We used to retrieve all the rows of columns prod_name contains text luzhaosahn

select prod_name
from products
where prod_name regexp 'luzhaoshan'
order by prod_name;


查询结果是:
prod_name
jetback    luzhaoshan

Why laborious use regular expressions it? Because in advanced queries in the usefulness of great good
example:

select prod_name
from products
where prod_name regexp '.000'
order by prod_name;

输出结果为:
prod_name

jetbaas    2000
hsahgj     8000
jaas    2000
ahgj     8000
jaas    2000
ahgj     8000

这里使用了正则表达式.000,是正则表达式语言中一个特殊的字符。它表示匹配任意一个字符,因此,2000和8000都匹配并且返回数据。
----当然这个也是可以使用like和通配符进行来完成的,如下参考:
select prod_name
from products
where prod_name like '%000'
order by prod_name;


对比来看:
select prod_name
from products
where prod_name regexp '.000'
order by prod_name;

----regexp 在列值内进行匹配
----like 匹配的是整个列
下面让你们看一个神奇的地方!!!!!!!!!!!!
select prod_name
from products
where prod_name like '000'
order by prod_name;


对比来看:
select prod_name
from products
where prod_name regexp '.000'
order by prod_name;

#### 如果执行上述两条语句,会发现第一条是不返回数据,而第二条语句返回一行,为什么??
因为,LIKE匹配整个列,如果被匹配的文本在列值内部中,并非是单独的列,LIKE 将不会找到它,相应的行也不会返回,【除非使用通配符获取数据】。


而REGEXP正好是在列值内部进行匹配,如果被匹配的文本在裂殖内部中,REGEXP就会找到它,相应的行也会返回。【这是一个非常重要的差别!!!】

REGEXP如果想要实现LIKE的功能,需要和^和$符合结合使用。

OR-match in regular expression | to represent the OR operation


select prod_name 
from products
where prod_name REGEXP  '1000|2000'
order by prod_name;

输出结果为:
prod_name

jetpack    1000
jetmaoc    2000

Statements using regular expressions 1000 | 2000. | As a regular expression or operator. It means match one of them. So both 1000 and 2000 and the return match.

Use | select statement or expression similar to be used from the function, conditions may be incorporated into a single or a plurality of regular expressions.

---- OR two or more conditions may be given more than two OR condition. For example,
'1000 | 2000 | 3000' to 1000 or 2000 or 3000 match.

Match the specified character, by [] characters to complete

E.g:


select prod_name
from products
where prod_name regexp '[123]ton'
order by prod_name;

输出结果是:
prod_name
1 ton 
2 ton

这里使用正则表达式[123]ton,[123]定义一组字符,它的意思是匹配1 或者匹配2 或者匹配3,因此 1ton 和 2 ton 都匹配并且返回数据,(没有3ton)

由此可知,[]是另一种形式的OR语句,事实上,正则表达式[123]ton为[1|2|3]ton
的缩写,也可以使用后者。结果都是一样的如下所示:
select prod_name
from products
where prod_name regexp '[1|2|3]ton'
order by prod_name;

输出结果是:
prod_name
1 ton 
2 ton

[0-9]匹配任意0-9的字符
[a-z]匹配任意a-z的任意字母
例子如下:
select prod_name
from products
where prod_name regexp '[1-5]ton'
order by prod_name;

输出结果为:
prod_name

1 ton anvil
2 ton anvil
3 ton anvil
.5 ton snkil

这里使用正则表达式[1-5]ton。定义了一个范围,这个表达式意思是匹配到1-5,因此返回3个匹配行。 由于 5 ton 匹配,所以返回    .5ton

If you want to find a number or a number with a decimal point of beginning, the beginning of all products, how to do a simple search [0-9 \.] Does not work, because it is to find a match anywhere in the text, the solution is to use positioning ^ symbol.


^     文本的开始
$     文本的结束

select prod_name
from products
where prod_name regexp '^[0-9]\\.'
order by  prod_name

输出结果为:
prod_name

1 ton anvil
2 ton anvil
3 ton anvil
.5 ton snkil

^ 匹配串的开始。
^的双重用途,
第一个是在集合中用来否定该集合。
第二个是用来指示串的开始处。


简单的正则表达式测试:
可以在不适用数据库表的情况下用select来测试正则表达式,regexp检查总是返回0
0是没有匹配,1是匹配,可以使用带文本串的regexp来测试表达式,并实验他们
相应的测试如下:
select ‘hello’ regexp  ‘[0-9]’;
这个例子显然将返回结果0,因为文本hello中没有数字。


Guess you like

Origin www.cnblogs.com/ludundun/p/11614010.html