使用正则表达式从mysql列中提取子字符串

时间:2017-02-28 05:04:58

标签: mysql regex substring

我的正则表达式是(pnr |(P | p)_.:,!"' / / _ _:,!!"' / / $) + [_。:,!"' - / $] + [0-9] {3} [_.:,,"' - / $] + [0- 9] {7}

从列中提取pnr编号。

示例文字: 94eb2c0cb17ef354bb052c57f40c \ r \ nConContent-Type:text / plain; charset = UTF-8 \ r \ nContent-Transfer-Encoding pnr:986-097832

94eb2c0cb17ef354bb052c57f40c \ r \ nContent-Type:pnr:986-097832 text / plain; charset = UTF-8 \ r \ nContent-Transfer-Encoding

pnr:986-097832 94eb2c0cb17ef354bb052c57f40c \ r \ nConContent-Type:text / plain;字符集= UTF-8 \ r \ nContent传送编码

我必须创建一个只提取pnr号的mysql查询

3 个答案:

答案 0 :(得分:1)

尝试 SUBSTRING_INDEX

SELECT 
SUBSTRING_INDEX(
SUBSTRING_INDEX('94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding pnr:986-097832', 'pnr:', -1), ' ',1);

SELECT 
SUBSTRING_INDEX(
SUBSTRING_INDEX('94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: pnr:986-097832 text/plain; charset=UTF-8\r\nContent-Transfer-Encoding', 'pnr:', -1), ' ',1);

SELECT 
SUBSTRING_INDEX(
SUBSTRING_INDEX('pnr:986-097832 94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding', 'pnr:', -1), ' ',1);

<强>样品

mysql> SELECT
    -> SUBSTRING_INDEX(
    -> SUBSTRING_INDEX('94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: pnr:986-097832 text/plain; charset=UTF-8\r\nContent-Transfer-Encoding', 'pnr:', -1), ' ',1);
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SUBSTRING_INDEX(
SUBSTRING_INDEX('94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: pnr:986-097832 text/plain; charset=UTF-8\r\nContent-Transfer-Encoding', 'pnr:', -1), ' ',1) |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 986-097832                                                                                                                                                                  |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

mysql>
mysql> SELECT
    -> SUBSTRING_INDEX(
    -> SUBSTRING_INDEX('pnr:986-097832 94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding', 'pnr:', -1), ' ',1);
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SUBSTRING_INDEX(
SUBSTRING_INDEX('pnr:986-097832 94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding', 'pnr:', -1), ' ',1) |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 986-097832                                                                                                                                                                  |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

mysql>

答案 1 :(得分:1)

SELECT REGEXP_SUBSTR(column, 'pnr:[0-9\-]{10}')
FROM table

答案 2 :(得分:-3)

对于Oracle,我们可以执行以下操作 -

    SQL> create table test2( id varchar2(2000));

    Table created.

    SQL> insert into test2 values ('pnr:986-097831 94eb2c0cb17ef354bb052c57f40c\r\nC
    ontent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding');

    1 row created.

    SQL> insert into test2 values('94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: pnr
    :986-097832 text/plain; charset=UTF-8\r\nContent-Transfer-Encoding')
      2  ;

    1 row created.

    SQL> insert into test2 values('94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: tex
    t/plain; charset=UTF-8\r\nContent-Transfer-Encoding pnr:986-097833');

    1 row created.

    SQL> select regexp_substr( id, '(P|p)(N|n)(R|r):[0-9]*\-[0-9]*' ) PNR FROM  test2;

    PNR
----------------

pnr:986-097831
pnr:986-097832
pnr:986-097833

    SQL>

`