Skip to content

re.search cannot find negative sign #105625

Closed as not planned
Closed as not planned
@impodog

Description

@impodog

Bug report

The regular expression I used is working wrongly.
I've made this test file, and found out that the negative sign cannot be searched.

import re

pattern = r"\b(-?\d+)\b"
content = "The temperature is -3 degrees Celsius."

match = re.search(pattern, content)
if match:
    number = match.group(1)
    print("Found:", number)
else:
    print("Not found.")

The result is expected to be "Found: -3", but it turns out to be "Found: 3".
I believe that something is wrong with the "-" in regular expressions.

Your environment

  • CPython versions tested on: python-3.11.4-amd64
  • Operating system and architecture: Win11 64-bit operating system, x64-based processor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions