regular expression multiple matches

北战南征 提交于 2019-12-11 01:59:43

问题


For reference, this is the regex tester I am using:
http://www.rsyslog.com/regex/

How can I modify this regular expression:

[^;]+

to receive multiple sub-matches for the following test string:

;first;second;third;fourth;fifth and sixth;seventh;

I currently only receive one sub-match:

first

Basically I want each sub-match to consist of the content between ; characters, I am hoping for a sub-match list like this:

first
second
third
fourth
fifth and sixth
seventh

回答1:


Following information given in the comments I discovered that the reason I can't get more than one sub-match is that I need to specify the global modifier - and I can't seem to figure out how to do that in the ryslog regex tester I am using.

However, this did lead me to solve my problem in a slightly different manner. I came up with this regular expression which still only gives one match, but the number near the end acts as the index for the desired match, so for example:

(?:;([^;]+)){5}

matches this from my test string in the question:

fifth and sixth

While this solution allows me to achieve what I wanted - though in a different manner - the true answer to my question is found in HamZa's comments. More specifically:

How can I modify the regular expression to receive multiple sub-matches?

The answer is, you can't modify the regular expression itself in order to get multiple sub-matches. Setting the global modifier is required in order to do that.

Based on this information I have posted a new question on serverfault targeted specifically to the rsyslog regular expression system.



来源:https://stackoverflow.com/questions/29111248/regular-expression-multiple-matches

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