Match any characters more than once, but stop at a given character

百般思念 提交于 2021-02-05 06:38:46

问题


I am writing a regex that will be used for recognizing commands in a string. I have three possible words the commands could start with and they always end with a semi-colon.

I believe the regex pattern should look something like this:

(command1|command2|command3).+;

The problem, I have found, is that since . matches any character and + tells it to match one or more, it skips right over the first instance of a semi-colon and continues going.

Is there a way to get it to stop at the first instance of a semi-colon it comes across? Is there something other than . that I should be using instead?


回答1:


The issue you are facing with this: (command1|command2|command3).+; is that the + is greedy, meaning that it will match everything till the last value.

To fix this, you will need to make it non-greedy, and to do that you need to add the ? operator, like so: (command1|command2|command3).+?;

Just as an FYI, the same applies for the * operator. Adding a ? will make it non greedy.




回答2:


Tell it to find only non-semicolons.

[^;]+



回答3:


What you are looking for is a non-greedy match.

.+?

The "?" after your greedy + quantifier will make it match as less as possible, instead of as much as possible, which it does by default.

Your regex would be

'(command1|command2|command3).+?;'

See Python RE documentation



来源:https://stackoverflow.com/questions/13005815/match-any-characters-more-than-once-but-stop-at-a-given-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!