import re
# Pattern to match integers with optional sign
pattern = r"[+-]?\d+"
# Example text containing integers
text = "The numbers are +42, -17, and 100."
matches = re.findall(pattern, text)
print(matches) # Output: ['+42', '-17', '100']
- The pattern
r"[+-]?\d+"
matches both positive and negative integers (+42
and -17
), as well as unsigned integers (100
).