Skip to content

Regular expression example not clear. #141953

@bg012677

Description

@bg012677

# Documentation
https://docs.python.org/3/library/re.html#re.Pattern

Using the data shown, it is not clear the difference between using "X" vs. "^X" as both results are the same.

Current text:
re.search("^X", "A\nB\nX", re.MULTILINE) # Match

re.search("^X", "A\nB\nX", re.MULTILINE)
<re.Match object; span=(4, 5), match='X'>
re.search("X", "A\nB\nX", re.MULTILINE)
<re.Match object; span=(4, 5), match='X'>

Recommended text change:
re.search("^X", "AX\nBX\nXC", re.MULTILINE) # Match

re.search("^X", "AX\nBX\nXC", re.MULTILINE) # Match
<re.Match object; span=(6, 7), match='X'>
re.search("X", "AX\nBX\nXC", re.MULTILINE) # Match
<re.Match object; span=(1, 2), match='X'>

With the ^ the search picks up the X at the beginning of the line at position 6.
With no ^ the search picks up the X at position 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions