I have this:
string input = @"(+order: top* OR +order: first* OR +order: second* OR +order: third* OR +order: ""fourth top"" OR +order: fifth*)";
I need to get a regex that extracts from the above as follows:
“top, first, second, third, fourth top, fifth”
I made this
public static string GetOrders(string input)
{
string pattern = @"order(.*)OR";
List<string> orders = new List<string>();
foreach (Match m in Regex.Matches(input, pattern))
orders.Add(m.Value);
return string.Join(", ", orders.ToArray());
}
My regex pattern is incomplete. I thought I can just extract everythin between “+order:” and “OR”, but it’s not working. It doesn’t seem to iterate the elements, I just get the whole input string.
What am I doing wrong?
.*is greedy, matching from the firstorderto the lastOR.@"order(.*?)OR"would work but only for the first four matches; the fifth one isn’t followed byOR.So a better regex would be
Even better (assuming that the rule is “either the parameter contains no spaces and ends with
*, or it contains spaces and is enclosed in"s”):