查找未由其他字符串继续的字符串



我正在尝试为Visual Studio的Find All功能提出正确的正则表达式,但是我遇到了非常困难的时光。我有以下字符串:

字符串 A:

other.things.go.here.ClientTemplate("#= Namespace.sub.nope.method(data) #");

字符串 B:

other.things.go.here.ClientTemplate("#= Namespace.sub.KeyPhrase.otherMethod(data) #");

字符串 C:

other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.KeyPhrase.otherMethod(data) #");

此正则表达式应仅与字符串 A 匹配。以下是我认为可行的方法:

(ClientTemplate).*~(KeyPhrase)

我需要匹配一个包含"客户端模板"的字符串,并且不会继续使用"KeyPhrase"。

编辑:我真的搞砸了这个。我的意思与我所说的相反:

什么正则表达式只匹配字符串 A

我认为你需要这样的模式

ClientTemplate.*KeyPhrase

演示

或者,如果你想要一个没有继续KeyPhrase的字符串,你可以使用这样的负面展望

ClientTemplate(?!.*KeyPhrase.*$).*$

演示

编辑:这个问题对我产生了影响,所以真正的答案在底部。 我离开原版是因为这样似乎不那么令人困惑,而且我认为即使正则表达式不是,大多数讨论仍然有帮助。


假设您在 2013 年之前的 Visual Studio 中使用正则表达式(我知道您将其标记为 visual-studio-2012,但许多新用户在真正谈论代码中的正则表达式时错误地添加了visual-studio*标签(,如果删除~,您的正则表达式似乎可以工作:

(ClientTemplate).*(KeyPhrase)

~(...)构造等效于负前瞻。 您说要匹配包含关键短语的行,因此您需要将其包含在正则表达式中,而不是排除它。 (这就是我删除正则表达式否定标签的原因。

但是,我会选择更具确定性的东西,如下所示:

ClientTemplate("#=[^"]*.KeyPhrase.[^"]*")

我在Visual Studio 2008中对此进行了测试,它匹配字符串B和C,但不匹配A。


编辑:事实证明,这毕竟是一项否定任务。 下面是一个匹配字符串 A 且不匹配字符串 B 和 C 的正则表达式:

<ClientTemplate("#= Namespace(.~(KeyPhrase>):i)*(data) #");

:i大致相当于w+,Perl衍生风格的一个词的传统正则表达式。 前瞻检查点后面的任何内容在允许:i>使用它之前都没有KeyPhraseClientTemplate之前的<KeyPhrase:i之后的>是词界;他们确保您只匹配整个单词。

在名为"my_file"的文件中考虑以下字符串

other.things.go.here.ClientTemplate("#= Namespace.sub.nope.method(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.nope.method(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.KeyPhrase.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.KeyPhrase.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.KeyPhraze.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.KeyPHRAZE.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.KeyPHR?ZE.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.keyphrase.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.keyphrase.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.slkjfiowe.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.dkjsehtw8.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.something.anything.11185.otherMethod(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.so...................

命令行

 grep  'other.things.go.here.ClientTemplate("#= Namespace.sub.nope.method(data) #");' '/root/Desktop/my_file' 

输出

other.things.go.here.ClientTemplate("#= Namespace.sub.nope.method(data) #");
other.things.go.here.ClientTemplate("#= Namespace.sub.nope.method(data) #");

命令行打印类似于字符串 A 的任何内容。

正则表达式模式可以简单地像这样

grep '^other.*go.here.ClientTemplate.*nope.method(data) #");' 'my_file'

最新更新