1

I have a single line of text which may contain 1 or more matches. I am using named capture groups since I am continuously changing things around, and this makes it easier.

String:

blah blah blah dasf{{optionA:B4}}t estsdf{{optionB:B4}}sadf{{optionB:B4}}sadf13

Pattern:

(?<choice>\{\{(?<c>optionA|optionB):(?<d>[A-Z]\d{1,2})\}\})

Every example I can find online which deals with this type of structure doesn't use named capture groups, and I haven't been able to puzzle it together myself.

In my example scenario, there are 3 matches, and for each match, I want to be able to access the content of the 'choice', 'c' and 'd' capture groups.

Can someone show me a simple example of how to do this?

3
  • Do you really mean backreferences? Or captures? Do you want to refer to the groups within the regex or after execution of the regex? Commented Dec 13, 2012 at 19:51
  • after execution, I basically need to know .Value for all these matches. Commented Dec 13, 2012 at 20:29
  • Alright, that is called capturing. Backreferences usually only refer to the things inside the regex or sometimes inside the replacement string (what Tim Pietzker showed in his answer) Commented Dec 13, 2012 at 20:32

1 Answer 1

4

To access the contents of named capturing groups after a match, you need to use a regex object:

Dim RegexObj As New Regex("(?<quote>['""])(?<text>.*?)\k<quote>")
Result = RegexObj.Match(Subject).Groups("text").Value

Now Result will contain the contents of the (?<text>...) capturing group.

For multiple matches, you can iterate over the results, calling .NextMatch() until the last match has been found:

Dim ResultList As StringCollection = New StringCollection()
Dim RegexObj As New Regex("(?<quote>['""])(?<text>.*?)\k<quote>")
Dim Result As Match = RegexObj.Match(Subject)
While MatchResult.Success
    ResultList.Add(Result.Groups("text").Value)
    Result = Result.NextMatch()
End While

The original answer to the question (which had been about backreferences, not capturing groups):

There are two situations where you can use backreferences:

  • To refer to a backreference within the same regex, use \k<groupname>.
  • To insert the text matched by a named group in the replacement text, use ${groupname}.

For example,

res = Regex.Replace(subject, "(?<quote>['""])(?<text>.*?)\k<quote>", "*${text}*")

will change

This is a "quoted text" and 'so is this'!

into

This is a *quoted text* and *so is this*!
Sign up to request clarification or add additional context in comments.

4 Comments

I understand what you are saying, but I am not understanding how it answers my question, as I don't see how it would allow me to access these values within VB .NET code.
@se_dude: Ah, sorry, I misunderstood. Is this better?
That's about as far as I have gotten. The issue is that I am dealing with multiple matches, and each match has 3 named groups. I really do appreciate your help!
While there were some issues with the code, it did set me on the right track, so thank you VERY much for taking the time to help me :) I would give you a vote, but can't vote yet. I was using MatchCollections before, which made things really complicated, while your method was a lot easier, and actually worked. Also learned something new (StringCollection) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.