Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将MySQL中的正则表达式与条件排除括号的重复单词匹配_Mysql_Sql_Regex - Fatal编程技术网

将MySQL中的正则表达式与条件排除括号的重复单词匹配

将MySQL中的正则表达式与条件排除括号的重复单词匹配,mysql,sql,regex,Mysql,Sql,Regex,我有个疑问。我使用mysql作为数据库。我想使用一个正则表达式来匹配我期望的结果,表是 table A ---------------------------------- | ID | Description | ---------------------------------- | 1 | new 2 new 2 new 2 new | | 2 | new 21 new 2 new | | 3 | new 2th 2

我有个疑问。我使用mysql作为数据库。我想使用一个正则表达式来匹配我期望的结果,表是

table A

----------------------------------
|   ID  | Description            |
----------------------------------
|   1   |  new 2 new 2 new 2 new |
|   2   |   new 21 new 2 new     |
|   3   |   new 2th 2 (2/2)      |
|   4   |   2new 2new (2/2)      |
|   5   |   new2 new 2new        |
我期望的结果
-数字2只能显示两次
-2后面/前面的字符必须是varchar(空格后面除外)
-特殊条件:可接受ID=3和ID=4等带有“(数字/数字)”图案的任何数字


到目前为止我试过的问题


可能有一种方法可以通过一个正则表达式实现这一点,但我发现使用三个正则表达式更容易:

select *
from a
where description regexp '[a-zA-Z ]+[0-9]+[a-zA-Z ]+[0-9]+' and
      (description not regexp '[0-9]+[^0-9]+[0-9]+[^0-9]+[0-9]+' or
       description regexp '[0-9]+[^0-9]+[0-9]+[^0-9]+[0-9]+/[0-9]+'
      );
编辑:

我没有意识到目标只针对
2
,而不是任何数字。这符合您的规则,但不符合您的结果(4不属于):

我建议这个正则表达式:

^([^2]|[[:<:]][0-9]+/[0-9]+[[:>:]])*([[:<:]]|[a-z])2([[:>:]]|[a-z])([^2]|[[:<:]][0-9]+/[0-9]+[[:>:]])+([[:<:]]|[a-z])2([[:>:]]|[a-z])([^2]|[[:<:]][0-9]+/[0-9]+[[:>:]])*$

在代码中

SELECT
    *
FROM
    A
WHERE 
    description REGEXP '^(([^2]+|[[:<:]][0-9]+/[0-9]+[[:>:]])*2([[:>:]]|[a-z])){2}([^2]+|[[:<:]][0-9]+/[0-9]+[[:>:]])*$'
单词边界匹配单词的开头和结尾。这里单词的定义是一系列字母、数字和下划线字符

[[[::]]
是一个单词的开头边界,因此在单词的末尾匹配


在这里使用它们可以确保
2
(以及数字/数字部分)不被其他数字包围(因此使得
21
例如失败),或者如果您有
21/4
,则将
2
计数为一个,并对字符串中的两个
2
进行计数。

对不起。。输入错误在您的第二个期望值中,除了空格,您的意思是什么?它不会在ID=2中显示结果,因为在2之后有1(2之前/之后的字符必须是varchar)-它会在ID=3中显示结果,因为在2之前/之后只有空格(除了空格之后/之前)id=4不属于你的规则。你所说的varchar是指字母表/字母,对吗?varchar是数据类型而不是字符类型,varchar可以包含任何字符。(数字/数字)是否可以有多个特殊条件?如
2new2new(2/2)(2/2)
或编号改变位置,如
(2/2)2new2new
^([^2]|[[:<:]][0-9]+/[0-9]+[[:>:]])*([[:<:]]|[a-z])2([[:>:]]|[a-z])([^2]|[[:<:]][0-9]+/[0-9]+[[:>:]])+([[:<:]]|[a-z])2([[:>:]]|[a-z])([^2]|[[:<:]][0-9]+/[0-9]+[[:>:]])*$
(2/2) 2new 2new
2new (2/2) 2new (2/2)
SELECT
    *
FROM
    A
WHERE 
    description REGEXP '^(([^2]+|[[:<:]][0-9]+/[0-9]+[[:>:]])*2([[:>:]]|[a-z])){2}([^2]+|[[:<:]][0-9]+/[0-9]+[[:>:]])*$'
^                                        # Beginning of string

(                                        # Open repeat group
   ([^2]+|[[:<:]][0-9]+/[0-9]+[[:>:]])*  # Any characters. See #1
  2                                      # 2
  ([[:>:]]|[a-z])                        # Word boundary or alphabet/letter. See #2
){2}                                     # Close repeat group and repeat 2 times

([^2]+|[[:<:]][0-9]+/[0-9]+[[:>:]])*     # Any characters. See #1

$
(           # Open group

  [^2]+     # Any characters except 2

|           # OR

  [[:<:]]   # Open word boundary
  [0-9]+    # Any numbers
  /         # Forward slash
  [0-9]+    # Any numbers
  [[:>:]]   # Close word boundary

)*          # Close group and repeat any number of times
(           # Open group
  [[:>:]]   # Word boundary
|           # Or
  [a-z]     # Letter/alphabet
)           # Close group