![](/img/trans.png)
[英]php regex to python, preg_match($x,$y,$z) to re.search(x,y,z)
[英]Using re.search in Python Regex to separate x and y coordinates
假設我有以下LaTex代碼段:
& $(-1,1)$
& $(0,\infty)$
我如何在python中使用正則表達式以將坐標對分離為其x和y分量? 我想為此使用re.search。
例如,對於以上部分,我希望收到:
x: -1 y: 1
x: 0 y: \infty
目前,我正在使用:
c = map(str,re.findall(r'-?\S',range))
a = c[1]
b = c[3]
但是,這僅匹配整數,而不匹配我需要的“ \\ infty”。
謝謝。
關於什么:
import re
lines = ''' & $(-1,1)$
& $(0,\infty)$ '''
matches = re.findall(r'\((.*),(.*)\)', lines)
for (a,b) in matches:
print "x: %s y: %s" % (a,b)
產出
x: -1 y: 1
x: 0 y: \infty
如果您感到有些奇怪,請考慮將*
替換為*?
使匹配“不貪心”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.