gpt4 book ai didi

正则表达式中的MySQL匹配空格

转载 作者:行者123 更新时间:2023-11-28 23:30:11 26 4
gpt4 key购买 nike

在匹配字符串中的空格时,我遇到了 Mysql 中正则表达式语法的问题。

我有一个邮政编码数据库,格式为:
1111 AA CITYNAME1111 CITYNAME

由此,我想提取邮政编码和城市名称,我使用了以下代码:

DROP FUNCTION IF EXISTS GET_POSTALCODE;
CREATE FUNCTION GET_POSTALCODE(input VARCHAR(255))
RETURNS VARCHAR(255)
BEGIN
DECLARE output VARCHAR(255) DEFAULT '';
IF input LIKE '^[1-9][0-9]{3}[[:blank:]][A-Z]{2}[[:blank:]]%'
THEN
SET output = SUBSTRING(input, 1, 7);
ELSE
SET output = SUBSTRING(input, 1, 4);
END IF;
RETURN output;
END

我希望将 9741 NE Groningen 的输入字符串的结果拆分为 9741 NEGroningen

但我得到的是 9741NE Groningen

我已经尝试了各种方法来匹配空白,我认为这是问题所在。我试过了:

  • [[:blank:]]
  • [:blank:]
  • [[:space:]]
  • [:space:]
  • \s方法

[:space:] 应该匹配所有空格,但结果还是一样。

我尝试的任何方法似乎都不起作用,您能否为我指明正确的方向?

谢谢!

最佳答案

前言

如果使用 this library,您可以启用 PCRE

描述

^([0-9]{4}(?:[[:blank:]]+[a-z]{2}(?=[[:blank:]]))?)[[:blank:]](.*$)

Regular expression visualization

此正则表达式将执行以下操作:

  • 找到后跟可选的两个字符的 4 位代码
  • 匹配字符串的其余部分,应该是城市名称

例子

现场演示

https://regex101.com/r/sE3xN7/4

示例文本

请注意,您的示例只有 4 位代码,所以我冒昧地添加了一个额外的数字

1111 AA CITYNAME1
2222 CITYNAME2
3333 Las Vegas
4444 BB Las Vegas
9741 NE Groningen

样本匹配

MATCH 1
1. [0-7] `1111 AA`
2. [8-17] `CITYNAME1`

MATCH 2
1. [18-22] `2222`
2. [23-32] `CITYNAME2`

MATCH 3
1. [33-37] `3333`
2. [38-47] `Las Vegas`

MATCH 4
1. [48-55] `4444 BB`
2. [56-65] `Las Vegas`

MATCH 5
1. [66-73] `9741 NE`
2. [74-83] `Groningen`

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[[:blank:]]+ whitespace (\n, \r, \t, \f, and " ")
(1 or more times (matching the most
amount possible))
----------------------------------------------------------------------
[a-z]{2} any character of: 'a' to 'z' (2 times)
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[[:blank:]] whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
[[:blank:]] whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------

关于正则表达式中的MySQL匹配空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37602211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com