Hi I am trying to use the solution from
Find all pattern indexes in string in C#
However, it doesn’t work in my situation
string sentence = "A || ((B && C) || E && F ) && D || G";
string pattern = "(";
IList<int> indeces = new List<int>();
foreach (Match match in Regex.Matches(sentence, pattern))
{
indeces.Add(match.Index);
}
It produces error , “parsing “(” – Not enough )’s”.
I am not sure what I have done wrong here.
Appreciate any help.
Thanks,
Balan Sinniah
You’ve forgotten that
(has a special meaning within regular expressions. If you useit should work, I believe. Alternatively, just keep using
string.IndexOfgiven that you’re not really using the pattern-matching of regular expressions.If you are going to use regular expressions, I’d personally create a
Regexobject rather than using the static method:That way there’s rather less scope for confusion around which argument is the input text and which is the pattern.