如何使用正则表达式从坐标对中提取x和y [英] How to extract x and y from a coordinate pair using regex

查看:386
本文介绍了如何使用正则表达式从坐标对中提取x和y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用正则表达式从(a,b)中提取a和b?

How would I use regex to extract a and b from (a,b)?

例如:

string = "(1,-5)"
string = "(0, \infty)"

然后输出将是:

x = 1
y = -5


x = 0
y = \infty

请注意,这不仅需要使用整数,还需要使用字符串.

Please note that this needs to work with more than just integers, but strings as well.

推荐答案

您可以使用 map 函数:

You can use re.findall and map function :

>>> string = "(1,-5)"
>>> map(int,re.findall(r'-?\d',string))
[1, -5]

模式\d将匹配您字符串中的任何数字!

The pattern \d will match any digits in your string!

这篇关于如何使用正则表达式从坐标对中提取x和y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆