DeveloperBreeze

Python Data Extraction. Development Tutorials, Guides & Insights

Unlock 1+ expert-curated python data extraction. tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your python data extraction. skills on DeveloperBreeze.

Understanding Regular Expressions with `re` in Python

Tutorial October 24, 2024
python

In cases where the numbers are scattered across a complex string, you can still extract them easily using the same pattern.

import re

# Pattern to match signed and unsigned integers/floats
pattern = r"[+-]?\d+(\.\d+)?"

# Complex string with embedded numbers
text = "In 2021, the growth was +6.5%, compared to -3.14% in 2020."

matches = re.findall(pattern, text)
print(matches)  # Output: ['2021', '+6.5', '-3.14', '2020']