regular expression multiple matches

2019-07-21 11:32发布

问题:

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.



标签: regex rsyslog