Sql 如何在oracle中拆分包含空格和特殊字符的字符串?
我想按空格和特殊字符(如果有的话)分割字符串。 Ex:用于表示移动交换中心信号强度 目前我正在使用正则表达式来拆分字符串,但我无法同时拆分空格和特殊字符Sql 如何在oracle中拆分包含空格和特殊字符的字符串?,sql,regex,oracle,substring,Sql,Regex,Oracle,Substring,我想按空格和特殊字符(如果有的话)分割字符串。 Ex:用于表示移动交换中心信号强度 目前我正在使用正则表达式来拆分字符串,但我无法同时拆分空格和特殊字符 insert into tmp(word) select regexp_substr('For expressing mobile switching center (signal strength).', '(.*?)([[:space:]]|$)', 1, level, null, 1 ) as token
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
更新代码:
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)
这是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]+|\(|\)|\.)')
我调整了您的regexp,以便它搜索一个括号字符或一系列不是空格或括号的字符
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:]()]+');
这将在任何非字母或数字字符处拆分:
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;
你需要包括在你的字符类,我尝试了这一点,并更新了上面的代码。看来我还需要修改。你能看一下吗?
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:]()]+');
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;