Reputation: 385
Suppose I have the following segment of LaTex Code:
& $(-1,1)$
& $(0,\infty)$
How would I use regex in python in order to separate out the coordinate pair into its x and y components? I want to use re.search for this.
For example, for the above segment I would want to receive:
x: -1 y: 1
x: 0 y: \infty
Currently I am using:
c = map(str,re.findall(r'-?\S',range))
a = c[1]
b = c[3]
However this only matches integers and not the "\infty" 's I need.
Thank you.
Upvotes: 0
Views: 158
Reputation: 30210
What about:
import re
lines = ''' & $(-1,1)$
& $(0,\infty)$ '''
matches = re.findall(r'\((.*),(.*)\)', lines)
for (a,b) in matches:
print "x: %s y: %s" % (a,b)
Outputs
x: -1 y: 1
x: 0 y: \infty
If you catch some weirdness, consider replacing *
with *?
to make the matching "not greedy".
Upvotes: 2