CerIs
CerIs

Reputation: 567

Regex group may not exist

I'm using regex in C# to extract parts of a URL, but if the URL doesn't have "www" then it changes the grouping count so I can't always go to match.Groups[3].Value to get my value. My regex is

 Regex r1 = new Regex(@"(http[s]?://)([A-Za-z0-9\-]+)(\.([A-Za-z0-9\-]+))*");

Can you make group 2 always exist so it doesn't break the count?

Upvotes: 0

Views: 1219

Answers (2)

ChrisC73
ChrisC73

Reputation: 1863

Use an optional passive (non-capturing) group (ie: '(?:www.)?' below):

Regex r1 = new Regex(@"(http[s]?://)(?:www\.)?([A-Za-z0-9\-]+)(\.([A-Za-z0-9\-]+))*");

The existence (or not) of 'www.' will then not factor into the grouping count.

Upvotes: 1

Peter Duniho
Peter Duniho

Reputation: 70701

Sorry, should read the question more closely before answering it. :)

If you name the groups, then you should be able to index them by name and check the "Success" property for each to see what elements were actually matched.

Of course, for some regex the whole thing won't match at all unless all of the capture groups had a match. It depends on the regex.

Upvotes: 0

Related Questions