Im trying to build a regular expression that captures any number (integer, float, with scientific notation or not). Im using groups so that if I need to update something I update only one line. Here's what I'm doing:
intNumber = r"(?P<Integer>-?(0|[1-9]+[0-9]*))" # Integer
floatNumber = r"(?P<Float>"+intNumber+r"\.[0-9]+)" # Float
sciNumber = r"(?P<Scientific>"+floatNumber+r"(e|E)(-|\+)?[0-9]+)" # Scientific
anyNumber = r"(?P<AnyNumber>"+sciNumber+"|(?P=Integer)|(?P=Float))" # Any number
The problem is that although each of the regex works on its own, when I combine them all in anyNumber
using or (|
) it captures only scientific notation numbers and not the rest. What am I doing wrong?
Edit: To refine my question, is it possible to have a dynamically generated regex (with the goal of simple single spot maintenance in mind) that also is flexible enough to allow me to use its components separately, without problems like redefinition of groups and with convenient naming of the groups? I know I may be asking too much..