I try to get miles and chains integer values from a string like "at (17.08)". The false decimal form of the input data is the choice of the data administrator. First I tried the following pattern on this string "17.08":
"((\d+)\.(\d\d))"
This behaved correctly:
group(0) is "17.08"
group(1) is "17.08"
group(2) is "17"
group(3) is "08"
but now for the "at (17.08) is a" variation: I want to be able to substitute a correctly formatted location for the "decimal" notation, with this pattern:
".*\(?((\d+)\.(\d\d))\)?.*"
When queried with re.match I get the following match groups:
group(0) is "(17.08)", OK.
group(1) is "7.08", where is the 1 going ?
group(2) is "7", where is the 1 going ?
group(3) is "08", still OK.
What am I doing wrong ? Why does "re" behave like this ? I have an idea this must be related to the "greedy"/"non-greedy" theme, but how ?
searchif you just intend to have one output. Use non capturing groups:(?:stuff)will make sure you don't capture subgroups within the regex itself.17, the08, or17.08? Also, on my machine, copying your pattern and example gives7.08, not17.08as group 1.