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

Regex examples should use "\z" not "$" for end-of-string #5422

Open
twylite opened this issue Mar 12, 2021 · 2 comments
Open

Regex examples should use "\z" not "$" for end-of-string #5422

twylite opened this issue Mar 12, 2021 · 2 comments
Labels
Milestone

Comments

@twylite
Copy link

twylite commented Mar 12, 2021

The documentation for Regex.IsMatch (in Regex.xml) includes an example pattern ^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$. It describes the trailing "$" as "End the match at the end of the line". This is not entirely accurate: Anchors in Regular Expressions says that "The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string", and testing confirms this behavior. This means that the example pattern accepts part number "1298-673-4192\n", which is a subtle validation bug.

Proposed fix: Use the anchor "\z" instead of "$".

This fix is relevant because Java, PCRE, and various other regex engines have "$" behave like dotnet's "\z". Regex.IsMatch should draw attention to the correct anchor, to help developers avoid validation bugs.

@PRMerger17 PRMerger17 added the Pri3 Indicates issues/PRs that are low priority label Mar 12, 2021
@dotnet-bot dotnet-bot added the untriaged New issue has not been triaged by the area owner label Mar 12, 2021
@antonfirsov
Copy link
Member

Tagging area owners @pgovind @tannergooding

@jzabroski
Copy link

jzabroski commented Jun 1, 2022

@twylite I actually put together a spec to improve this, but got sidetracked a good bit with long haul COVID.

dotnet/runtime#25598

In the issue, Dan Moseley actually does a great job spec'ing out how to think about this.

@joperezr joperezr added this to the Backlog milestone Aug 2, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants