Reputation: 4656
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:
I expect to get the following groups:
I get the result for the first string, but fore the 2 others I get:
What can be the issue?
Upvotes: 2
Views: 148
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
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