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

时间:2014-04-06 01:55:32

标签: regex plsql oracle11g

--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.

问题:

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

正则表达式的作用是什么?

1 个答案:

答案 0 :(得分:2)

这个正则表达式有效:

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

Regular expression visualization

Debuggex Demo

Call me Ishmael 之后的文本和后面的任何句号,逗号或空格都会被放入捕获组1中。

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

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


似乎Oracle regex没有字边界。所以试试这个:

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

Regular expression visualization

Debuggex Demo