Regex for selecting repeating groups

I have a series of grouped values ​​that follow a specific format and would like to use a single expression to group them. For example, I have -group1 -group2 -group3 and I'm trying to use something similar to (-[\s\S]{1,}?) . This basically allows me to write the entire line to one group, but I would like to be able to track each of the values ​​separately. I figured that ? make it be inanimate and, therefore, split the pattern match into three separate groups (for example). Right now I'm just repeating the link (-[\s\S]*?) , But there seems to be a more elegant expression.
Thank!

+7
regex
Jun 15 '12 at 13:26
source share
3 answers

You're lucky because C # is one of the few languages ​​(if not the only one) that supports subexpressions, captures

https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.capture(v=vs.110)

API.NET can be seen as follows

  Matches Groups (most regex engines stop here) Captures (unique for .NET) 

It’s not clear from your question that you want to match exactly, but this should get you started. Ask again if you are stuck.

  string input = "-group1 -group2 "; string pattern = @"(-\S*\W){2}"; foreach (Match match in Regex.Matches(input, pattern)) { Console.WriteLine("Match: {0}", match.Value); for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++) { Group group = match.Groups[groupCtr]; Console.WriteLine(" Group {0}: {1}", groupCtr, group.Value); for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++) Console.WriteLine(" Capture {0}: {1}", captureCtr, group.Captures[captureCtr].Value); } } 

It outputs

 Match: -group1 -group2 Group 0: -group1 -group2 Capture 0: -group1 -group2 Group 1: -group2 Capture 0: -group1 Capture 1: -group2 

As you can see (group 1, capture 0) and (group 1, capture 1) offer separate entries for the group (and not the last, as in most languages)

In this address, I think of what you are describing as "being able to reverse highlight each of the values ​​separately"

(You use the term "backreference", but I don’t think you are aiming to replace the template correctly?)

+14
Jun 15 2018-12-12T00:
source share
— -

With .NET regex (and almost exclusively .NET) you can use:

 (?:(-\S+)\s*)+ 

Group 1 will contain a list of all subscripts.

Or maybe just using Matches enough in your case:

 var re = new Regex(@"-\S+"); var matches = re.Matches(str); 
+2
Jun 15 '12 at 13:35
source share

Try the following:

 (-.+?)(\s|$) 

Your first capture group will have what you want ( -group1 , -group2 , etc.).

If you need more control over what to resolve after - , change .+? for example, [a-zA-Z0-9]+? to match only alphanumeric characters.

0
Jun 15 '12 at 13:32
source share



All Articles