2017-04-23 73 views
-1

当试图解析字符串时,提及的字符串是我正在使用的正则表达式:.*@${username}.*其中${username}是一个参数。匹配“hello @c”而不是“hello @cat”的正则表达式

如果${username} = 'c'

那么这将成为.*@c.* 所以这将不正确匹配类似hello @cat

我需要匹配整个字符串正则表达式:

@c 
hello @c 
@c hello 
hello @c hello 

和不匹配:

@cat 
  • 它不必是一个正则表达式。它可以是多个覆盖所有情况而不会干扰另一个案例的案例。
+0

这似乎工作:'(^ | \ s)@c(\ s | $)'。可能是一堆更好的方法来做到这一点,而不是一个正则表达式大师。 – Tibrogargan

+1

可能重复[正则表达式匹配整个单词](http://stackoverflow.com/questions/1751301/regex-match-entire-words-only) – JJJ

+0

@JJJ问题与\ b是'@'字符,所以它是不是同一个问题 – Tibrogargan

回答

0

只要确保用户名后面没有用户名称后缀:@$username\([^a-z]\|$\)。请注意,根据正则表达式解析器,您可能需要删除反斜杠。