8000 Regex examples should use "\z" not "$" for end-of-string · Issue #5422 · dotnet/dotnet-api-docs · GitHub
[go: up one dir, main page]

Skip to content
Regex examples should use "\z" not "$" for end-of-string #5422
Open
@twylite

Description

@twylite

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0