A regex pattern that matches all forms of integers and decimal numbers in Python The question is pretty self-explanatory. I wrote a regex pattern which should (in theory) match all possible integers and decimal numbers. The pattern is as follows:

import re

pattern = '^[+-]?((\d+(\.\d*)?)|(\.\d+))$'
re.compile(pattern)


How foolproof is this pattern? I tested out quite a few scenarios, and they all worked fine. Am I missing some edge case here? Thanks for any help.
Your expression looks just fine, maybe we would slightly modify that to:
^[+-]?((\d+(\.\d+)?)|(\.\d+))$

for failing these samples, 3., 4., for instance, just in case maybe such samples might be undesired. Other than that, you have some capturing groups that I'm guessing you'd like to keep those.

Test the capturing groups with re.finditer
import re

regex = r&quot;^[+-]?((\d+(\.\d+)?)|(\.\d+))$&quot;

test_str = (&quot;0.00000\n&quot;
    &quot;0.00\n&quot;
    &quot;-200\n&quot;
    &quot;+200\n&quot;
    &quot;200\n&quot;
    &quot;200.2\n&quot;
    &quot;-200.2\n&quot;
    &quot;+200.2\n&quot;
    &quot;.000\n&quot;
    &quot;.1\n&quot;
    &quot;.2\n&quot;
    &quot;3.\n&quot;
    &quot;.&quot;)

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print (&quot;Match {matchNum} was found at {start}-{end}: {match}&quot;.format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print (&quot;Group {groupNum} found at {start}-{end}: {group}&quot;.format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))


Test with re.findall
import re

regex = r&quot;^[+-]?((\d+(\.\d+)?)|(\.\d+))$&quot;

test_str = (&quot;0.00000\n&quot;
    &quot;0.00\n&quot;
    &quot;-200\n&quot;
    &quot;+200\n&quot;
    &quot;200\n&quot;
    &quot;200.2\n&quot;
    &quot;-200.2\n&quot;
    &quot;+200.2\n&quot;
    &quot;.000\n&quot;
    &quot;.1\n&quot;
    &quot;.2\n&quot;
    &quot;3.\n&quot;
    &quot;.&quot;)

print(re.findall(regex, test_str, re.MULTILINE))

The expression is explained on the top right panel of this demo, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.

RegEx Circuit
jex.im visualizes regular expressions: