-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
DotNet 6 Preview 7: Regex.Match and Regex.IsMatch wrong results with startat parameter overload #58794
Comments
Tagging subscribers to this area: @eerhardt, @dotnet/area-system-text-regularexpressions Issue DetailsDescriptionWhen using Regex.Match(string input, int startat)
Regex.IsMatch(string input, int startat) It finds no match, contrary to using: Regex.Match(input.Substring(startat))
Regex.IsMatch(input.Substring(startat)) in which case match is found. Shouldn't overloads taking in I don't want to use
|
@fitdev - can you provide a program that reproduces the issue with expected and actual results? |
Not necessarily. For example, if the pattern contains constructs that look before the current position (e.g. a lookbehind), there's a large difference between using startat to start matching in the middle of a string (where the engine is able to look at the content of the input before the startat) and trimming off everything that came before startat (in which case the engine is unable to look there). Another example is anchors, where an anchor can have a very specific meaning about its position relative to the beginning of the input. |
That's tracked by #23602, which also discusses why from a public API perspective it'd be possible to add an |
Have you tried using the overload that takes the beginning position and a length, rather than a startat position? (Yes, it's confusing from an API perspective that two overloads differ in the meaning of an |
@stephentoub Thank you very much for your fast response and detailed explanation.
Yes I have and noticed that it behaves as expected. However the problem is that there is such an overload only for So I guess I will have to stick with One last point then: the docs should then be improved and clearly explain this subtle behavior difference, both between using |
If they don't already, I agree that'd be useful. Note the docs are open source; if you'd like to submit a PR to do so, that'd be welcome. That said, they do try to make this clear, in the details Remarks section for both overloads, e.g.
I'd need to measure, but it's likely to depend on how long the input string and how much you're trimming.
Please feel free to open an issue proposing such an overload. Seems reasonable for Match and IsMatch to have a consistent set of overloads.
Thanks. Glad you enjoyed it. |
Description
When using
It finds no match, contrary to using:
in which case match is found.
Shouldn't overloads taking in
int startat
parameter behave the same as ifinput.Substring(startat)
was passed instead?I don't want to use
Substring
workaround since this leads to unnecessary allocations and copying. In that reagrd it would be nice if Regex methods would haveSpan
-based overloads.The text was updated successfully, but these errors were encountered: