[英]Using re.search in Python Regex to separate x and y coordinates
Suppose I have the following segment of LaTex Code: 假设我有以下LaTex代码段:
& $(-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? 我如何在python中使用正则表达式以将坐标对分离为其x和y分量? I want to use re.search for this.
我想为此使用re.search。
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. 但是,这仅匹配整数,而不匹配我需要的“ \\ infty”。
Thank you. 谢谢。
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". 使匹配“不贪心”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.