regexp_replace()
假设行的格式相同,这是一种适用于 10g 的使用方法:
with tbl(col_string) as
(
select 'There is something 2015.06.06. in the air 1234567 242424 2015.06.07. 12125235'
from dual
)
select regexp_replace(col_string, '^.*(\d{4}\.\d{2}\.\d{2})\. \d*$', '\1')
from tbl;
正则表达式可以读作:
^ - Match the start of the line
. - followed by any character
* - followed by 0 or more of the previous character (which is any character)
( - Start a remembered group
\d{4}\.\d{2}\.\d{2} - 4 digits followed by a literal period followed by 2 digits, etc
) - End the first remembered group
\. - followed by a literal period
- followed by a space
\d* - followed by any number of digits
$ - followed by the end of the line
regexp_replace 然后用第一个记住的组 (\1) 替换所有这些。
基本上将整行描述为正则表达式,围绕您想要返回的内容进行分组。如果它可能是数字以外的其他字符,您很可能需要调整行尾的正则表达式,但这应该会给您一个想法。
为了争论,只有在日期模式出现 2 次的情况下,这才有效:
with tbl(col_string) as
(
select 'There is something 2015.06.06. in the air 1234567 242424 2015.06.07. 12125235' from dual
)
select regexp_substr(col_string, '\d{4}\.\d{2}\.\d{2}', 1, 2)
from tbl;
返回模式的第二次出现。我希望上面的 regexp_replace 更准确地描述了解决方案。