简体   繁体   English

正则表达式:查找大于特定值的数字(具有不同的小数长度)

[英]Regex: find numbers greater than specific value (with varying decimal lengths)

I am trying to regex find all values in a list that are greater than or equal to .03. 我正在尝试正则表达式查找列表中大于或等于.03的所有值。 The tricky part is that my values have between 9 and 15 decimal places. 棘手的部分是我的值在9到15位小数之间。

My current code works somewhat but is unwieldy - any advice much appreciated: 我当前的代码有点工作但是不实用 - 任何建议都非常感激:

^(?:0?\.[0-9][3-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]|0?\.[0-9][3-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]|0?\.[0-9][3-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])$

Thank you. 谢谢。

You can use an asterisk to denote zero or more of a digit: 您可以使用星号表示零个或多个数字:

^(?:0?\.\d[3-9]\d*)$

This has the added benefit of matching exactly 0.03 or something with (say) 100 decimal places. 这具有额外的好处,即精确匹配0.03或具有(例如)100个小数位的东西。

If you want to be strict about the decimal places, you can use brace syntax, which matches any repetitions between 9 and 15 digits: 如果你想严格控制小数位,你可以使用大括号语法,它匹配9到15位之间的任何重复:

^(?:0?\.\d[3-9]\d{9,15})$

Note that, as written, this regex doesn't match anything greater than or equal to 0.03--it matches anything greater than or equal to 0.03 and less than 1, and it doesn't match, say, 0.1, which is greater than 0.03. 请注意,如上所述,此正则表达式不匹配大于或等于0.03的任何内容 - 它匹配大于或等于0.03且小于1的任何内容,并且它不匹配,例如0.1,大于0.03。 To match anything greater than 0.03, best to skip regex entirely and parse the number. 要匹配大于0.03的任何内容,最好完全跳过正则表达式并解析数字。

You should simply parse your data as float : 您应该简单地将数据解析为float

From your pattern you seem to have the numbers already seperated very well - why use regex at all? 从你的模式来看,你似乎已经将这些数字分离得很好 - 为什么要使用正则表达式呢? You numbers fill the whole line or aren't matched at all ( r"^........$" so essentially you have: 你的数字填满整行或根本不匹配( r"^........$"所以基本上你有:

t = """0.0000002
0.4
0.04
0.004
24544.23354
Also Bad"""

Simply split into lines and check each line as float 简单地分成几行,并将每一行检查为浮点数

# splits the string into a list of lines
for line in (x.strip() for x in t.split("\n")):
    try:
        if float(line) >= 0.03:
            print(line)
        else: 
            print("Not ok:",line)
    except:
        print("Not ok:",line)

Output: 输出:

Not ok: 0.0000002
0.4
0.04
Not ok: 0.004
24544.23354
Not ok: Also Bad

Here you go: 0.030000000 - 0.999999999999999 你去:0.030000000 - 0.999999999999999

Greater than or equal to .03 and less than 1.0 大于或等于.03且小于1.0
at 9-15 decimal places 在小数点后9-15位

0?\.(?:03\d{7,13}|0[4-9]\d{7,13}|[1-9]\d{8,14})

Expanded 扩展

 #  0.030000000  -  0.999999999999999
 0?
 \.
 (?:
      03 \d{7,13} 
   |  0 [4-9] \d{7,13} 
   |  [1-9] \d{8,14} 
 )

Note - this was machine generated, there could be some overlap. 注意 - 这是机器生成的,可能会有一些重叠。
Example: 0?\\.(?:0[3-9]\\d{7,13}|[1-9]\\d{8,14}) 示例: 0?\\.(?:0[3-9]\\d{7,13}|[1-9]\\d{8,14})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM