我要做的是匹配字符串,如果该字符串包含X个以@#$:字符开头的单词(比如5个)。你知道吗
假设X为5的示例:
@someword someotherword anotherword word1 word2 word3 => false
@someword :someotherword #anotherword $word1 word2 word3 => false
@someword :someotherword #anotherword $word1 #word2 $word3 => true
Tags:
积极的展望将是实现这一目标的一种方法:
这个问题很棘手,因为您想用一个特殊的符号
[@#$:]
来匹配单词。但是,我们不能只使用单词边界\b
,因为特殊字符不是单词字符。因此,我们可以检查在目标项开始之前的是空格,还是字符串的最开始。你知道吗您可以使用此正则表达式,前提是这些符号仅在单词字符之前使用:
RegEx Demo
代码:
\B
:匹配\b
不匹配的地方[@#$:]\w+
:匹配1+个以[@#$:]
开头的单词字符[^@#$:]*
:匹配0个或多个不包含[@#$:]
的字符(...){5}
:在当前输入中匹配5像这样的?你知道吗
相关问题 更多 >
编程相关推荐