regular expressions groups
-
hi, 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: 2) member/filter-two/option, /filter-two/option, empty string 3) member/filter-three/option/option, /filter-three/option/option, empty string What can be the issue?
-
hi, 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: 2) member/filter-two/option, /filter-two/option, empty string 3) member/filter-three/option/option, /filter-three/option/option, empty string What can be the issue?
We do have a Regular expressions forum[^] - you should use that in future. But, it's a Sunday, and I'm feeling generous! :laugh: If you name your groups instead of using anonymous groups it becomes clearer what the problem is:
member/filter(?.*)(?<Option>/[^/]+)*
Shows that all of the matches are in the Prefix group: nothing gets as far as the Option group because the "any character, repeated" clause in the prefix swallows the option text as well. Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
hi, 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: 2) member/filter-two/option, /filter-two/option, empty string 3) member/filter-three/option/option, /filter-three/option/option, empty string What can be the issue?
I second using the correct forum in the future.
benams wrote:
(/.+)*
I've never gotten that sort of thing to work, so I expect it won't. Try
@"member/filter([^/]*)/([^/]*)/([^/]*)"
(And I like to name my groups too.)