用于浮点数或整数的 Python 正则表达式,同时不将浮点数拆分为两个浮点数 [英] Python regex for float or int while not splitting the float into two floats
问题描述
我正在尝试从一个可以是 int 或 float 的文件中提取数据.我发现这个正则表达式将从文件 (\d+(\.\d+)?)
中提取这两种类型,但我遇到的问题是它将浮点数拆分为两个.
这样做的目的是获取由 (start nn) 定义的起始坐标,但正如你所看到的,它取了 218.948 并将其拆分为 218.948
和 .948代码>.
126.111
也有同样的问题.
如果输入字符串的起始括号中有一个 int,我得到以下内容:
<预><代码>>>>line = "(gr_line (start 218.948 126) (end 218.948 143.637) (angle 90) (layer Edge.Cuts) (width 0.1) (tstamp 53D2B530))">>>打印 re.findall(r'\(start (\d+(\.\d+)?) (\d+(\.\d+)?)\)', line)[('218.948', '.948', '126', '')]>>>这里的问题是添加的空索引 - 不是一个大问题,但有点不方便.
我如何格式化我的正则表达式,以便它捕获一个浮点数并返回该浮点数,或一个 int 并返回该整数.
您正在使用 (\d+(\.\d+)?)
试试这个:
(\d+(?:\.\d+)?)
这只会保存整个浮动中的分组.
I'm trying to pull data from a file that can either be an int or a float. I've found this regex that will pull these two types from the file (\d+(\.\d+)?)
, but the problem I'm having with it is that it's splitting the floats into two.
>>> import re
>>> line = "(gr_line (start 218.948 126.111) (end 218.948 143.637) (angle 90) (layer Edge.Cuts) (width 0.1) (tstamp 53D2B530))"
>>>
>>> print re.findall(r'\(start (\d+(\.\d+)?) (\d+(\.\d+)?)\)', line)
[('218.948', '.948', '126.111', '.111')]
>>>
The purpose of this is to get the starting coordinates which are defined by (start n n), but as you can see, it's taking 218.948 and splitting it into 218.948
and .948
. Same issue with 126.111
.
If the input string has an int in the starting brackets, I get the following:
>>> line = "(gr_line (start 218.948 126) (end 218.948 143.637) (angle 90) (layer Edge.Cuts) (width 0.1) (tstamp 53D2B530))"
>>> print re.findall(r'\(start (\d+(\.\d+)?) (\d+(\.\d+)?)\)', line)
[('218.948', '.948', '126', '')]
>>>
The issue here is the added empty index - not a huge problem, but a little inconvenient.
How can I format my regex so it captures either a float and return that float, or an int and return that int.
You're capturing and saving two groupings with (\d+(\.\d+)?)
Try this:
(\d+(?:\.\d+)?)
That will only save the grouping from the entire float.
这篇关于用于浮点数或整数的 Python 正则表达式,同时不将浮点数拆分为两个浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!