则不匹配完整的单词

则不匹配完整的单词

本文介绍了如果满足否定的前瞻性,则不匹配完整的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要匹配那些没有特殊字符(如@:)的单词.例如:

I need to match only those words which doesn't have special characters like @ and :.For example:

  1. [email protected]不匹配
  2. list应该返回有效的匹配项
  3. show也应返回有效的匹配项
  1. [email protected] shouldn't match
  2. list should return a valid match
  3. show should also return a valid match

我使用负前瞻\w+(?![@:])

但是它匹配[email protected]中的gi,但它也不应该匹配.

But it matches gi out of [email protected] but it shouldn't match that too.

推荐答案

您可以将\w添加到前瞻:

\w+(?![\w@:])

等效项使用单词边界:

\w+\b(?![@:])

此外,您可以考虑添加一个左边界,以避免匹配非单词非空白文本块中的单词:

Besides, you may consider adding a left-hand boundary to avoid matching words inside non-word non-whitespace chunks of text:

^\w+(?![\w@:])

(?<!\S)\w+(?![\w@:])

^将与字符串开头的单词匹配,而(?<!S)仅在单词以空格或字符串开头开头时匹配.

The ^ will match the word at the start of the string and (?<!S) will match only if the word is preceded with whitespace or start of string.

请参见 regex演示.

为什么不(?<!\S)\w+(?!\S) 空白边界?因为由于要构建词法分析器,所以您最有可能必须处理自然语言句子,其中的单词可能后面带有标点符号,并且(?!\S)否定前瞻仅在\w+与空格或空格匹配时才匹配.在字符串的末尾.

Why not (?<!\S)\w+(?!\S), the whitespace boundaries? Because since you are building a lexer, you most probably have to deal with natural language sentences where words are likely to be followed with punctuation, and the (?!\S) negative lookahead would make the \w+ match only when it is followed with whitespace or at the end of the string.

这篇关于如果满足否定的前瞻性,则不匹配完整的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:12