如何使用正则表达式从坐标对中提取 x 和 y
How to extract x and y from a coordinate pair using regex
如何使用正则表达式从 (a,b) 中提取 a 和 b?
例如:
string = "(1,-5)"
string = "(0, \infty)"
然后输出将是:
x = 1
y = -5
x = 0
y = \infty
请注意,这不仅需要处理整数,还需要处理字符串。
您可以使用re.findall
and map
函数:
>>> string = "(1,-5)"
>>> map(int,re.findall(r'-?\d',string))
[1, -5]
模式 \d
将匹配字符串中的任何数字!
?
表示0或1,\d
表示数字,+
表示一个或多个。 \.
匹配点(因为 .
是没有反斜杠的特殊字符),*
表示 0 或更多。
my_string = '(1,-52.42)'
x, y = re.findall(r'-?\d+\.?\d*', my_string)
print(x, y)
您可以使用 ast.literal_eval
而不是正则表达式:
>>> import ast
>>> my_string = "(1,-5)"
>>> x,y = ast.literal_eval(my_string)
>>> x
1
>>> y
-5
如果您对科学记数法很在意,那么是的,它会起作用:
>>> my_string = "(1.2e3,-5.56)"
>>> x,y = ast.literal_eval(my_string)
>>> x
1200.0
>>> y
-5.56
这对于正则表达式来说会很棘手,因为数字的语法可以包含非数字。例如,3.8e8
是合法数字,但有一个 .
和一个 e
:
>>> 3.8e8
380000000.0
这意味着带有 \d
的正则表达式将不起作用:
>>> re.findall('\d','(1.2e3,4.5e6)')
['1', '2', '3', '4', '5', '6']
最好只拆分逗号上的字符串,然后去掉括号:
>>> def coords(string):
... x, _, y = string.partition(',')
... return x.lstrip('('), y.rstrip(')')
...
>>> coords('(123,456)')
('123', '456')
当然,这也很脆弱,因为那里可能有空格等。如果您没有具体说明什么是合法的,什么是不合法的,就很难处理输入。
如果您希望它是合法的 Python 输入,那么;最好只使用 literal_eval,如 .
中所建议
老笑话
Some people, when confronted with a problem, think
"I know, I'll use regular expressions." Now they have two problems.
我不推荐使用正则表达式,我发帖只是为了好玩。
import re
matcher = re.compile('\(\s*([+-]?\d+(\.\d+(e[+-]?\d+)?)?)\s*,\s*([+-]?\d+(\.\d+(e[+-]?\d+)?)?)\s*\)')
result = matcher.match('( +4.5e-4, -5.4e+4 )')
x = float(result.group(1)) # 0.00045
y = float(result.group(4)) # -54000.0
PS. 我喜欢 Joshua Taylor 发布的拆分和剥离解决方案。
如何使用正则表达式从 (a,b) 中提取 a 和 b?
例如:
string = "(1,-5)"
string = "(0, \infty)"
然后输出将是:
x = 1
y = -5
x = 0
y = \infty
请注意,这不仅需要处理整数,还需要处理字符串。
您可以使用re.findall
and map
函数:
>>> string = "(1,-5)"
>>> map(int,re.findall(r'-?\d',string))
[1, -5]
模式 \d
将匹配字符串中的任何数字!
?
表示0或1,\d
表示数字,+
表示一个或多个。 \.
匹配点(因为 .
是没有反斜杠的特殊字符),*
表示 0 或更多。
my_string = '(1,-52.42)'
x, y = re.findall(r'-?\d+\.?\d*', my_string)
print(x, y)
您可以使用 ast.literal_eval
而不是正则表达式:
>>> import ast
>>> my_string = "(1,-5)"
>>> x,y = ast.literal_eval(my_string)
>>> x
1
>>> y
-5
如果您对科学记数法很在意,那么是的,它会起作用:
>>> my_string = "(1.2e3,-5.56)"
>>> x,y = ast.literal_eval(my_string)
>>> x
1200.0
>>> y
-5.56
这对于正则表达式来说会很棘手,因为数字的语法可以包含非数字。例如,3.8e8
是合法数字,但有一个 .
和一个 e
:
>>> 3.8e8
380000000.0
这意味着带有 \d
的正则表达式将不起作用:
>>> re.findall('\d','(1.2e3,4.5e6)')
['1', '2', '3', '4', '5', '6']
最好只拆分逗号上的字符串,然后去掉括号:
>>> def coords(string):
... x, _, y = string.partition(',')
... return x.lstrip('('), y.rstrip(')')
...
>>> coords('(123,456)')
('123', '456')
当然,这也很脆弱,因为那里可能有空格等。如果您没有具体说明什么是合法的,什么是不合法的,就很难处理输入。
如果您希望它是合法的 Python 输入,那么;最好只使用 literal_eval,如
老笑话
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
我不推荐使用正则表达式,我发帖只是为了好玩。
import re
matcher = re.compile('\(\s*([+-]?\d+(\.\d+(e[+-]?\d+)?)?)\s*,\s*([+-]?\d+(\.\d+(e[+-]?\d+)?)?)\s*\)')
result = matcher.match('( +4.5e-4, -5.4e+4 )')
x = float(result.group(1)) # 0.00045
y = float(result.group(4)) # -54000.0
PS. 我喜欢 Joshua Taylor 发布的拆分和剥离解决方案。