【问题标题】:how to split a string with both space and special characters in oracle?如何在oracle中拆分包含空格和特殊字符的字符串?
【发布时间】:2020-01-15 07:08:21
【问题描述】:

我想用空格和特殊字符(如果有的话)分割字符串。 例如:用于表示移动交换中心(信号强度)。

目前我正在使用正则表达式来拆分字符串,但无法同时实现空格和特殊字符的拆分。

insert into tmp(word)
    select     regexp_substr('For expressing mobile switching center 
    (signal strength).', '(.*?)([[:space:]]|$)', 1, level, null, 1 ) as token
    from       dual
    connect by level <= regexp_count('For expressing mobile switching center (signal strength).', '[[:space:]/:]+') + 1

CREATE TABLE TMP(WORD VARCHAR2(4000));

Current Output: For
expressing
mobile
switching
center
(signal
strength).

Expected Output: For
expressing
mobile
switching
center
(
signal
strength
)
.

更新代码:

insert into tmp(word)
select     regexp_substr('For expressing mobile switching center (signal strength).', '(.*?)([[:space:]()]|$)', 1, level, null, 1 ) as token
from       dual
connect by level <= regexp_count('For expressing mobile switching center (signal strength).', '(.*?)([[:space:]()]|$)')+ 1

Result:
For
expressing
mobile
switching
center
(null)
signal
strength
.
(null)
(null)

【问题讨论】:

  • 你需要在你的角色类中包含()Regex Demo
  • 我试过这个并更新了上面的代码。看来我还需要修改。你能看一下吗?

标签: sql regex oracle substring


【解决方案1】:

这是 DB2,但希望你能翻译一下:

with data (s) as (values
('For expressing mobile switching center (signal strength).')
),
     tally (n) as (
select row_number() over (order by 1)
from   (values (0),(0),(0),(0),(0),(0),(0),(0)) x (n)
cross  join (values (0),(0),(0),(0),(0),(0),(0),(0)) y (n)
)
select regexp_substr(s,'([A-Za-z]+|\(|\)|\.)', 1, n)
from   data
cross  join tally 
where  n <= regexp_count(s,'([A-Za-z]+|\(|\)|\.)')

【讨论】:

    【解决方案2】:

    我调整了您的正则表达式,因此它正在搜索 (a) 括号字符或 (b) 一系列不是空格或括号的字符。

    select     regexp_substr('For expressing mobile switching center (signal strength).', '[()]|[^[:space:]()]+', 1, level, null) as token
    from       dual
    connect by level <= regexp_count('For expressing mobile switching center (signal strength).', '[()]|[^[:space:]()]+');
    

    【讨论】:

      【解决方案3】:

      这将在任何不是字母或数字的字符处拆分:

      WITH
          aset AS( SELECT 'abc def;ghi|hkl () 0W-' AS tobesplit FROM DUAL ),
          splitup ( VALUE, rest ) AS
              (SELECT SUBSTR( tobesplit
                            , 1
                            , REGEXP_INSTR( tobesplit || ' ',  '[^a-zA-Z0-9]' ) - 1 )
                    , SUBSTR( tobesplit, REGEXP_INSTR( tobesplit || ' ',  '[^a-zA-Z0-9]' ) + 1 ) || ' '
                 FROM aset
               UNION ALL
               SELECT SUBSTR( rest
                            , 1
                            , REGEXP_INSTR( rest,  '[^a-zA-Z0-9]' ) - 1 )
                    , SUBSTR( rest, REGEXP_INSTR( rest || ' ',  '[^a-zA-Z0-9]' ) + 1 )
                 FROM splitup
                WHERE rest IS NOT NULL)
      SELECT value 
        FROM splitup where value is not null;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        相关资源
        最近更新 更多