我试图找出目录中所有文件中所有文件的混凝土单词(方法调用API)的所有情况。我需要一个REGEXP来查找所有不包含更新的调用(新API)的事件。你能帮我吗?

例子:

  • 弃用API:方法(A,B,C)
  • 新API:方法({A:A,B:B:B,C:C})

REGEXP应找到所有包含“方法”但不是“方法”的文件({'。

谢谢你。

有帮助吗?

解决方案

我会说正确的方法是使用否定的外观操作员, ?!

/method(?!\(\{)/

上述状态:“任何事件的发生 method 那是 不是 其次是 ({"

它比建议的更好满足您的要求 /method([^{]/ 因为后者不匹配字符串结束(即 abc abc method)并且它无法处理两个字符的组合 ({ 您要求很好。

其他提示

betelgeuse:tmp james$ echo " method(a,b,c) "> test1
betelgeuse:tmp james$ echo " method(a,b,c) " > test3
betelgeuse:tmp james$ echo " method({a:a, b:b, c:c})" > test2
betelgeuse:tmp james$ grep "method([^{]" test*
test1: method(a,b,c) 
test3: method(a,b,c) 

解释: [ ] 定义字符类 - 即,该位置中的字符可以匹配类中的任何内容。

^ 作为班级的第一个字符是否定的:这意味着此类与任何字符匹配 除了 此类定义的字符。

{ 当然,在这种情况下,我们关心不匹配的唯一角色。

因此,在某些人中,这将与任何具有字符的字符串匹配 method( 其次是任何角色 除了 {.

您还可以做其他方法:

betelgeuse:tmp james$ grep "method(\w" test*
test1: method(a,b,c) 
test3: method(a,b,c)

\w 在这种情况下,(假设C语言环境)等效于 [0-9A-Za-z]. 。如果要允许可选空间,可以尝试:

betelgeuse:tmp james$ grep "method([[:alnum:][:space:]]" test*
test1: method(a,b,c) 
test3: method( a, b, c) 
betelgeuse:tmp james$ 

(在Grep语法中, [:alnum:] is the same asw;:空间:refers to any whitespace character - this is represented ass`在大多数正则实现中)

您可以使用 角色类 排除 下列的 {, ,例如

/method\([^{]/
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top