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

MySQL Matching whitespace in Regex

在匹配字符串中的空格时,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

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

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

我尝试的方法似乎都不起作用,你能给我指出正确的方向吗?

谢谢!

前进

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

描述

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

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

  • 找到后跟可选的两个字符的 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 :
----------------------------------------------------------------------
    [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 
----------------------------------------------------------------------
  [[:blank:]]                 whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  (                        group and capture to :
----------------------------------------------------------------------
    .*                       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 
----------------------------------------------------------------------