I've been using the Advent of Code as an excuse to get familiar with RegexBuilders.
In Day 4's example we're given a list of values like 2-4,6-8 which should be treated as two ranges. One from 2 through 4, and the other from 6 through 8.
I have a working solution for both parts, but I'd like to refine my parsing logic if possible.
Here's how I'm currently parsing each set of ranges:
After a quick skim over the Swift standard library (where SE-0351 promises that mapOutput(_:) should be defined) as well as over the Experimental String Processing Library I couldn't find this method anywhere. Maybe it got forgotten in the implementation? @Michael_Ilseman
I also ran into a similar issue while completing the Advent of Code challenge. Based on an example here embedding captures should work, but in my code it did not.
The following code typechecks but produces a runtime error (Could not cast value of type 'Swift.Substring' to '(Swift.Substring, Swift.Int, Swift.Int)')
Oddly enough, I'm also here because of Advent of Code! It was today's challenge (5) that got me to try RegexBuilder; sadly, I don't see any way to get what I'm after in this case. Given this builder code:
The capture output is overwritten with each repetition, and only the last value is preserved. This is typical of regex engines, so I'm not surprised, but I was hopeful that there might just maybe be a way to get a collection back. Alas.
If there's a different way to structure this to get the desired result, feel free to educate me!
In case anyone was wondering, the extra nils on the end of the output there are from additional regex not shown in the code above (to capture subsequent "move" information from the challenge's input).
I've run into this apparent limitation multiple times, too.
It seems that Regex / RegexBuilder just doesn't support matching a dynamic number of items, although I'd love to be mistaken about that.
I'd love to get not just the more concise code but the faster failure in the event of invalid input, from being able to match e.g. a list of numbers in the regex itself, rather than merely matching the overall character pattern and having to separately split [out the individual numbers] and map [them to Int or whatever].
It seems like it wouldn't be hard to add to the API - e.g. OneOrMore could have a reduce parameter that functions very much like either of the similar Sequence methods.