benams
benams

Reputation: 4656

Unexpected regular expression groups

I want to use regular expressions for analyzing a url, but I can't get the regex groups as I would expect them to be. My regular expression is:

@"member/filter(.*)(/.+)*"

The strings to match:

  1. "member/filter-one"
  2. "member/filter-two/option"
  3. "member/filter-three/option/option"

I expect to get the following groups:

  1. member/filter-one, /filter-one
  2. member/filter-two/option, /filter-two, /option
  3. member/filter-three/option/option, /filter-three, /option(with 2 captures)

I get the result for the first string, but fore the 2 others I get:

  1. member/filter-two/option, /filter-two/option, empty string
  2. member/filter-three/option/option, /filter-three/option/option, empty string

What can be the issue?

Upvotes: 2

Views: 148

Answers (2)

user1088520
user1088520

Reputation:

The "member/filter([^/]*)(/.+)*" seems logical but is impractical as it accepts empty options (i.e. member/filter1/////////). A more accurate-practical pattern which also allows you to accept more than one filter with options is member(/filter[^/]+(/[^/]+)*)*

Upvotes: 0

Saeed Neamati
Saeed Neamati

Reputation: 35852

Try

@"member/filter([^/]*)(/.+)*"

Another way could be to use the MatchCollection this way:

string url = "member/filter-three/option/option";
url = url.Replace("member/filter-", string.Empty); // cutting static content
MatchCollection matches = new Regex(@"([^/]+)/?").Matches(url);
foreach (Match match in matches)
{
    Console.WriteLine(match.Groups[1].Value);
}
Console.ReadLine();

Here, you first remove the constant part from your string (it could be a parameter of a function). Then you simply check for everything inside two / characters. You do that by identifying [^/] as the character you want to match, which means match one character, that is not a /, then put an identifier after that (+ sign), which means, match more than one character.

Upvotes: 2

Related Questions