I want to parse certain information from given patterns, which are as follows:
/root/test/subfolder/
relative/folder/
index.html
/root/test/style.css
test/test2/test3/testN/
I created a regex but it doesn’t match multiple strings like root/, test/ only the last instance. My code:
Regex re = new Regex(@"^(/?)([\w\.]+/)*([\w\.]+)?$");
foreach (Group gr in re.Match("/templates/base/test/header.html").Groups)
Console.WriteLine(gr + " @ " + gr.Index.ToString());
Console.ReadKey();
I want to have first slash as optional, then path with / at the end and optional filename at the end.
Repeated capturing groups always capture only the last repetition. But you can capture the entire repeated group instead (and use non-capturing parentheses
(?:...)for the repeated group:This will work in other regex flavors, too. .NET offers another feature, though: It is possible to access the individual matches of a repeated capturing group. Using your regex: