-
Notifications
You must be signed in to change notification settings - Fork 1.7k
RegExp.firstMatch should have a start argument #26815
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
Comments
All methods taking a string should also take Alternatively we should make it cheap to create a substring, so you wouldn't worry about doing it and passing the substring to the function. I think the latter will give us better APIs in the long run. |
Are you sure you don't want to use If you try your patterns until you find a match, and the matching is allowed to search, you won't necessarily find the earliest match in the string first. E.g., if you search for the patterns |
I ended up doing something quite different (I use allMatches for all the patterns and then verify there are no overlapping matches). I agree that the algorithm I initially described is bogus. I still think it's weird that you can't say "find the first match for this pattern after this point", though. :-) |
You can use use |
Sure but then why do we have firstMatch at all? |
Good point. We only have it on |
We should move these APIs from using an index to indicate the start to using an iterator or whatever solution we come up with for #28404. |
Just bumped into this. The suggestions dont account for empty matches. So the code ends up looking like:
Which is pretty gnarly. |
As usual, adding an extra parameter to the Can use something like: extension PatternFirstMatch on Pattern {
Match? firstMatch(String source, [int start = 0]) =>
pattern.allMatches(source, start).firstOrNull;
} This adds a (Would still be nice to update the |
Oh no apologies required! Just useful to share fully fledged solutions for future travellers! I had not thought of using firstOrNull either so helpful thanks. |
I want to try a bunch of patterns from one start index, then try them all again from the end position of the first match, if there was a match, until I get to the point where there are no matches.
The only way to do this right now is to use allMatches with a start index and then see if the result has any values and if so return the first one. This is rather awkward.
It would be cleaner if I could just use "firstMatch" with a start index. (For completeness, hasMatch and matchString also should have a start argument.)
Note that "matchAsPrefix" has a start index argument, but that doesn't help because it implies an anchored match at the start of the pattern. This would only work if the patterns all started with
.*?
, which they don't.The text was updated successfully, but these errors were encountered: