正则表达式匹配除最后一个空格外有空格的连续单词序列 [英] Regular expression matching a sequence of consecutive words with spaces except for the last space
问题描述
我有一个单词列表,我想匹配这些单词的任意组合.假设我有单词 apple
、orange
和 mango
,并且我正在使用以下字符串:
I have a list of words and I want to match any combination of those words. Assume that I have the words apple
, orange
and mango
and I am working with the following string:
This place has the best apple pie. They also have orange, apple and mango-apple smoothie ...
我目前的正则表达式是 \b((apple|orange|mango)[\s-(,\s)]*)+
The regular expression that I have so far is \b((apple|orange|mango)[\s-(,\s)]*)+
它匹配正确的单词组合,但另外匹配序列末尾的额外空格.我得到的匹配:
It matches the right combination of words but additionally it matches an extra space at the end of the sequence. The matches I get:
"apple "
"orange, apple "
"mango-apple "
我知道它为什么这样做.如何更改正则表达式以去掉末尾的最后一个空格?
I know why it does that. How can I change the regular expression to get rid of that last space at the end?
推荐答案
在末尾添加 \b
似乎对我有用.此外,-
放在方括号中时应放在最后.因此,您需要做的就是将正则表达式更改为:\b((apple|orange|mango)[\s(,\s)-]*)+\b
Adding a \b
at the end seems to have worked for me. Also, the -
should be put last when placed in square brackets. So, all you need to do is to change your regex to this: \b((apple|orange|mango)[\s(,\s)-]*)+\b
根据你的评论,我已经试过了:\b((apple|orange|mango)([\s,-]+(apple|orange|mango))*)+\b代码>.您当前的正则表达式的问题在于,您还在末尾添加了空格,以便您可以匹配
apple mango
例如.
As per your comment, I have tried out this: \b((apple|orange|mango)([\s,-]+(apple|orange|mango))*)+\b
. The problem with your current regex is that you are also throwing in the spaces at the end so that you could match apple mango
for instance.
我建议的正则表达式应该匹配空格、破折号或逗号,当且仅当它们后跟单词 apple
、orange
或 mango
时>.
The regex I propose should match the spaces, dashes or commas if and only if they are followed by the words apple
, orange
or mango
.
这篇关于正则表达式匹配除最后一个空格外有空格的连续单词序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!