How to get regex matches between optional start and end tokens

I have a massive string with occurrences of the strings do() and don't(). I have to find all text before a don't(), ignore all text after it, until another do() or end of string, then all text from that do() until another don't() or end of string. So far I have this (.*)don't\(\).*do\(\)(.*) and I'm processing it like this:
var rx = new Regex(@"(.*)don't\(\).*do\(\)(.*)");
var matches = rx.Matches(Input);
foreach (Match match in matches)
{
total += MulsTotal(match.Groups[1].Value);
total += MulsTotal(match.Groups[2].Value);
}
var rx = new Regex(@"(.*)don't\(\).*do\(\)(.*)");
var matches = rx.Matches(Input);
foreach (Match match in matches)
{
total += MulsTotal(match.Groups[1].Value);
total += MulsTotal(match.Groups[2].Value);
}
I'm not getting the expected answer this way, and am deeply suspicious that my regex is missing something. The input string is 20k long so I'm battling a bit figuring this out by hand.
6 Replies
Angius
Angius5d ago
I think writing a simple parser might be a better idea here, tbh
VoidPointer
VoidPointerOP5d ago
It's for 1 part of an AoC day challenge. Wrting a proper parser would be a nice exercise, but maybe for another time. What I can do here is just use find instances of the 2 enclosing tokens and loop substrings between them. I am a little too regex fixated. It looks like it is supposed to. The problem statement says:
Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.
It doesn't mention surplus dos like yours at sit do()amet. The function that will process your string with the do() doesn't care about either of these two tokens.
Angius
Angius5d ago
I just finished writing a parser btw :KEKW:
VoidPointer
VoidPointerOP5d ago
I've never written a real one in my 25 year career I wrote a Logo parser 1st year uni. That's about it
Angius
Angius5d ago
"Parser" just sounds grandiose. It's just a loop, really Loop over the string and keep saving the chars, if you detect don't() stop saving them, start saving them again when you detect do()
VoidPointer
VoidPointerOP5d ago
Yeah, thanks guys, I am doing it with a loop after all. Much simpler.

Did you find this page helpful?