简体   繁体   English

Python正则表达式字母数字字符串,其数字部分介于两个值之间

[英]Python regex alpha-numeric string with numeric part between two values

I am terrible at regex in general, but I would be interested to know if there is a method to check if the numeric part of an alpha-numeric string is between two values, or less/greater than a certain value? 我通常在regex上很糟糕,但是我想知道是否有一种方法可以检查字母数字字符串的数字部分是否在两个值之间,或者小于/大于某个值?

For example if I have a string to search in a file which has multiple numeric variations like below: 例如,如果我有一个字符串要在具有多个数字变化的文件中进行搜索,如下所示:

key_string (870 bytes)
key_string (1500 bytes)
key_string (70 bytes)

Is it possible to extract the 'key_string' string only on whether the '(xxxx bytes)' part is between a certain threshold, or less/greater than a certain value? 是否可能仅在“(xxxx字节)”部分在某个阈值之间,或小于/大于某个特定值之间时才提取“ key_string”字符串?

For example if I want to find all the above 'key_string' example where the second part is below 1200 bytes, can I print out: 例如,如果我想查找上述所有“ key_string”示例,其中第二部分低于1200个字节,我可以打印出:

key_string (870 bytes)
key_string (70 bytes)

and ignore the string below in one regular expression? 并在一个正则表达式中忽略下面的字符串? :

key_string (1500 bytes)

You can use re.findall() to search along with regex. 您可以使用re.findall()与正则表达式一起搜索。

Explanation of regex as below: 正则表达式说明如下:

key_string\s+\((\d+)\s+bytes\)

正则表达式可视化

Debuggex Demo Debuggex演示

Code: 码:

import re

with open('result.txt') as fh:
    for l in fh:
        a = re.findall(r"key_string\s+\((\d+)\s+bytes\)",l.strip())
        if len(a) > 0 and int(a[0]) < 1200:
            print (l)

Output: 输出:

C:\Users\dinesh_pundkar\Desktop>python c.py
key_string (870 bytes)

key_string (70 bytes)

C:\Users\dinesh_pundkar\Desktop>

Code 2 as suggested by @WiktorStribiżew : @WiktorStribiżew建议的代码2:

import re

pattern = r'key_string\s+\((\d+)\s+bytes\)'
regex = re.compile(pattern, re.IGNORECASE)
with open('result.txt') as fh:
    for match in regex.finditer(fh.read()):
        if int(match.group(1)) < 1200:
            print((match.group()))

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

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