繁体   English   中英

匹配的正则表达式后跟一个或多个单词?

[英]Regex for match followed by one or more words?

--Return rows that have one or more words that follow a string that
--begins with 'Call me Ishmael.'


with string_row as
( select 'Call me Ishmael.'                as line_1 from dual union all     --False
  select 'Call me Ishmael. Some years ago   .' as line_1 from dual union all --True
  select 'Call me Ishmael  Some years ago   .' as line_1 from dual union all --True
  select 'Call me Ishmael,  Some years ago   .' as line_1 from dual union all --True
  select 'Call me Ishmael .'                 as line_1 from dual union all    --False
  select 'Twas the best of times. Some years ago.' as line_1 from dual union all --False
  select 'Call me Ishmael     '             as line_1 from dual  -- False
)  
select line_1
        from string_row where 
        regexp_like(line_1,'Call me Ishmael\w{1,}')  --original attempt.
        regexp_like(line_1,'Call me Ishmael[., ]+\b([\w .,]+)') --Attempt that appears to work on Javasrcipt.

问题:

为什么这个正则表达式不起作用?

什么正则表达式工作?

这个正则表达式有效:

Call me Ishmael[., ]+\b([\w .,]+)

正则表达式可视化

Debuggex演示

Call me Ishmael 以及随后的任何句号,逗号或空格之后的文本被放入捕获组1中。

你的正则表达式的问题, Call me Ishmael\\w{1,}

它是否试图匹配任何字母,数字或下划线( \\w )中的一个或多个( {1,} ,相当于+ )。 您的许多行都包含逗号,空格和句点,因此需要对其进行扩展以捕获这些字符。


似乎Oracle正则表达式没有字边界。 所以试试这个:

Call me Ishmael[., ]+(\w[\w .,]*)

正则表达式可视化

Debuggex演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM