2016-09-21 94 views
0

我有这个正则表达式/^!balance ([^\s]+){3}/i。基本上,字符串应该以!balance开头,一个空格,然后恰好有三个单词,每个空格分隔一个空格。这可以工作,但要求字符串在字符串的末尾也有一个空格。只有在字符串末尾不匹配字符组

!balance whatever whatever whatever ---> matches, but requires space after the last 'whatever'

我应该添加什么,以便它匹配时没有空格?

+0

'/ ^!balance([^ \ s] + | $){3}/i'如果你还是行尾? – sisanared

回答

2

只需将正则表达式转换为/ ^!balance([^ \ s] +){3}/i即可在单词的开头要求空格。这将通过将空间移动到匹配组中来平衡空间。

+3

或稍短一点\/^!余额(\ S +){3}/i' –