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:
- “member/filter-one”
- “member/filter-two/option”
- “member/filter-three/option/option”
I expect to get the following groups:
- member/filter-one, /filter-one
- member/filter-two/option, /filter-two, /option
- 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:
- member/filter-two/option, /filter-two/option, empty string
- member/filter-three/option/option, /filter-three/option/option, empty string
What can be the issue?
Try
Another way could be to use the
MatchCollectionthis way: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.