3

I want to match all the literals in the form literal ( -- i.e. : Literal followed by space, then opening parenthesis. But if the literal is either of "hi" or "hello" or "bye", then it should not be matched.

So I am looking for the following result :

Literal     ::      Result
--------------------------------    
Hello (     ::      Match
There (     ::      Match
hello (     ::      Not Match
New (       ::      Match
hi (        ::      Not Match

I am trying to do it by lookahead regex. So I put like

(^|\s)(?!((hello|hi|bye)(\s\()))

But its matching all.

And I can't do it by lookbehind as it doesn't take regex expression.

Is there any regex to do this task?

UPDATE

I'm trying with perl and checkstyle (Don't know which flavor checkstyle uses).

The lookahead is giving Match for both.
But in lookbehind, Perl is giving error Variable length lookbehind not implemented in regex m/(?<!(hello|hi|bye))\s\(/, whereas in checkstyle I'm getting desired result.

4

3 回答 3

2

您的正则表达式不起作用,因为它总是匹配文字和之间的空格((因为空格匹配(^|\s)并且(不匹配((hello|hi|bye)(\s\()))。它还应该匹配许多其他地方的空间。

测试以显示您的匹配项

这个正则表达式应该工作:

\b(?!(?:hello|hi|bye)\s)\w+\s\(

测试这个正则表达式

解释:

\b- 单词边界。
(?!(?:hello|hi|bye)\s)- 负前瞻hellohibye后跟空格。
它后面是一个空格,所以我们匹配byelo (,如果不需要,请将其删除。
(?:hello|hi|bye)与简单地(hello|hi|bye)将其设置为非捕获组相反,它不会更改输出。
\w+- 一个或多个单词字符(单词字符通常是[A-Za-z0-9_])。
\s- 空间。
\(- 一个支架。

于 2013-08-22T13:11:40.850 回答
1

如果您使用的是与 perl 兼容的regex引擎,您应该能够使用像这样的零宽度否定后向断言...

(?<!hello|hi|bye) \(

使用 R 的示例(打开 perl 兼容性)...

string <- c( "hello (" , "hi (" , "bye (" , "Hello (" , "Anything (" )
grepl( pattern = "(?<!hello|hi|bye) \\(" , string , perl = TRUE )
[1] FALSE FALSE FALSE  TRUE  TRUE

我们可以像这样更精确一点......

^.+(?<!^hello|^hi|^bye)\s\(

匹配字符串的开头,然后是可选的任何字符,但不是hellohi或者bye在字符串的开头,然后是空格,然后是左括号。

于 2013-08-22T13:04:33.043 回答
0

我的猜测是——

压缩:

 (?:^|(?<=\s))((?!(?:hello|hi|bye)\s\()[a-zA-Z]+\s\()

展开:

 (?:
      ^ 
   |  (?<= \s )
 )
 (
      (?!
           (?: hello | hi | bye )
           \s \(
      )
      [a-zA-Z]+ \s \(
 )
于 2013-08-22T18:25:39.103 回答