Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find all occurrences of a parser in a string, ignoring unwanted (garbage) characters #181

Open
pmarflee opened this issue Dec 8, 2024 · 0 comments

Comments

@pmarflee
Copy link

pmarflee commented Dec 8, 2024

Consider the following piece of text, taken from one of the problems in this year's Advent of Code:

xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

I'm struggling to figure out how to find all instances of mul(x, x) instructions in the text, ignoring all the garbage characters. This is something that would be easy to do with Regular Expressions; the pattern mul\((\d+),(\d+)\) would find the matches and capture the values.

I have the following parser which will match instances of the instruction:

Terms.Text("mul(")
  .SkipAnd(
      Terms.Integer()
          .AndSkip(Literals.Char(','))
          .And(Terms.Integer()))
  .AndSkip(Literals.Char(')'))

But I can't figure out how to process the rest of the text. I guess that I need to use a OneOrMany parser, in conjunction with a parser which can churn through and discard the garbage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant