Regex to match only the coordinate tuple in text

Viewed 80

I have the following text sequence:
#76=CARTESIAN_POINT('',(0.,0.,0.));
#77=CARTESIAN_POINT('',(0.07,0.0999999999999997,1.));
#78=CARTESIAN_POINT('',(0.32,0.0999999999999997,1.));
#79=CARTESIAN_POINT('',(0.07,0.0999999999999997,0.));
#80=CARTESIAN_POINT('',(0.32,0.0999999999999997,0.));
#81=CARTESIAN_POINT('',(0.07,0.0999999999999997,1.));
#82=CARTESIAN_POINT('',(0.07,0.0999999999999997,1.));
#83=CARTESIAN_POINT('',(0.07,0.0999999999999997,0.));

And in this sequence, I want to extract the coordinates contained in the inner parentheses, for example, I want to extract (0.,0.,0.) from the first line and so on.
For this, I am using the following regular expression: coords = re.findall(",\(.+\)", line)
Please note that all this will be inside a for loop and hence the variable line would only contain a single line from that sequence above at a time.
The code that I write gives me the following output for the first line:
,(0.,0.,0.))

Is the expression I've written incorrect? What should be the right expression to only get the first ending parenthesis only? Thank You.

3 Answers

Any 1+ symbols except ( and ) inside ( and )

\([^()]+\)

Use

re.findall(r'\((\d+\.?\d*),(\d+\.?\d*),(\d+\.?\d*)\)', test_str)

See Python proof:

import re
test_str = r"#76=CARTESIAN_POINT('',(0.,0.,0.));\n#77=CARTESIAN_POINT('',(0.07,0.0999999999999997,1.));\n#78=CARTESIAN_POINT('',(0.32,0.0999999999999997,1.));\n#79=CARTESIAN_POINT('',(0.07,0.0999999999999997,0.));\n#80=CARTESIAN_POINT('',(0.32,0.0999999999999997,0.));\n#81=CARTESIAN_POINT('',(0.07,0.0999999999999997,1.));\n#82=CARTESIAN_POINT('',(0.07,0.0999999999999997,1.));\n#83=CARTESIAN_POINT('',(0.07,0.0999999999999997,0.));"
print(re.findall(r'\((\d+\.?\d*),(\d+\.?\d*),(\d+\.?\d*)\)', test_str))

Results:

[('0.', '0.', '0.'), ('0.07', '0.0999999999999997', '1.'), ('0.32', '0.0999999999999997', '1.'), ('0.07', '0.0999999999999997', '0.'), ('0.32', '0.0999999999999997', '0.'), ('0.07', '0.0999999999999997', '1.'), ('0.07', '0.0999999999999997', '1.'), ('0.07', '0.0999999999999997', '0.')]

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \.?                      '.' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \d*                      digits (0-9) (0 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  ,                        ','
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \.?                      '.' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \d*                      digits (0-9) (0 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  ,                        ','
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \.?                      '.' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \d*                      digits (0-9) (0 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  \)                       ')'
Related