gnychis
gnychis

Reputation: 7555

Misunderstanding groups in regex, is this possible?

I think I am misunderstanding how groups work in regular expressions and C#. But, I am still wondering if what I want is possible.

Let's take the following string

:::name(uploadFileName)

Which is matched by the expression

\A:::[a-zA-Z0-9_-]+\([a-zA-Z0-9_-]+\)

What I want to be able to do is easily extract out something like property = name and then value = uploadFileName.

I thought that this would happen with groups, where match.Groups[0].Value would be name and match.Groups[1].Value would be uploadFileName. Based on the definition of groups, I thought that would be true:

Grouping constructs delineate the subexpressions of a regular expression and capture the substrings of an input string.

However, match.Groups[0].Value is simply the whole string :::name(uploadFileName)

Upvotes: 1

Views: 74

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627022

0th group is the entire match. Other matches start with 1. However, your regex does not capture anything, as there are no unescaped parentheses. Here is what you could use in case you do not have enclosed parenthetical groups. I'd suggest using named captures here for simplicity:

\A:::(?<name>[a-zA-Z0-9_-]+)\((?<property>[a-zA-Z0-9_-]+)\)

Then, you'll have your results in match.Groups["name"].Value (=name) and match.Groups["property"].Value (= uploadFileName).

Upvotes: 5

Amber
Amber

Reputation: 526883

The zeroth item is the entire string matched by the regex. The indices after that are the contents of each captured group. So for what you want, you need two capture groups:

\A:::([a-zA-Z0-9_-]+)\(([a-zA-Z0-9_-]+)\)

Then match.Groups[1].Value will be name and match.Groups[2].Value will be uploadFileName. Note the double parentheses for the second part - the \( and \) are matching literal parentheses characters, whereas the non-escaped ones are delineating the actual capture group for the regex.

Upvotes: 4

Related Questions