gpt4 book ai didi

python - 使用 Python re 的多模式嵌套正则表达式

转载 作者:行者123 更新时间:2023-12-04 10:28:20 25 4
gpt4 key购买 nike

使用 re 库在 Python 中编写此 sed 程序的等效方法是什么?这种 sed 模式一次完成搜索,而且效率很高。我正在尝试提取 cpu 的型号。请在底部查看我的 Python 代码尝试。

样本输入:

processor       : 0
vendor_id : GenuineIntel
cpu family : 6
model : 45
model name : Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
stepping : 6

输出:
E5-2660

示例输入 2:
processor       : 127
vendor_id : AuthenticAMD
cpu family : 23
model : 1
model name : AMD EPYC 7601 32-Core Processor
stepping : 2

输出:
EPYC 7601

塞德:
/AuthenticAMD/{
s/.*/AMD/p
}
/GenuineIntel/ {
n
n
n
/Celeron/ {
s/.*\([egptEGPT][1-9][0-9][0-9][0-9][a-zA-Z][a-zA-Z]\).*/\1/p
s/.*\([egptEGPT][1-9][0-9][0-9][0-9][a-zA-Z]\).*/\1/p
s/.*\([egptEGPT][1-9][0-9][0-9][0-9]\).*/\1/p
q
}
/Xeon/ {
s/.*[eE][3579]-\([1-9][1-9][1-9][1-9]\).*/\1/p
s/.*\([eElL]C[1-9][0-9][0-9][0-9]\).*/\1/p
s/.*\([35][0-9][0-9][0-9]\).*/\1/p
q
}
}

在 Python 中尝试(不工作):

我的代码搜索每个表达式并且不遵循任何嵌套规则,这效率不高。寻找更好的方法来写这个。
string = """processor       : 0
vendor_id : GenuineIntel
cpu family : 6
model : 45
model name : Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
stepping : 6"""

pattern = r'''GenuineIntel.*
(?=Celeron
.*([egptEGPT][1-9][0-9][0-9][0-9][a-zA-Z][a-zA-Z]).*
.*([egptEGPT][1-9][0-9][0-9][0-9][a-zA-Z]).*
.*([egptEGPT][1-9][0-9][0-9][0-9]).*)|
(?=Xeon
.*([eE][3579]-[1-9][0-9][0-9][0-9]).*)'''

print(re.search(pattern, string, re.MULTILINE|re.DOTALL|re.VERBOSE).groups())

最佳答案

拥有像 Python 这样的全功能语言和结构良好的数据,我不会尝试使用正则表达式解析所有内容。相反,我只是写了一个代码来完成这项工作,只在最后使用正则表达式。这种方式代替了大量的正则表达式,我用非常简单的正则表达式编写了简短易读的代码。

data = {}
for line in string.split("\n"):
left, right = line.split(":")
data[left.strip()] = right.strip()

if data["vendor_id"] == "GenuineIntel":
model = data["model name"]
if "Xeon" in model:
code = re.search(r"\bE\d-\d{4}\b", model, re.I).group(0)
elif "Celeron" in model:
code = re.search(r"\b[EGPT]\d{4}[a-z]{0,2}\b", model, re.I).group(0)

print(code)

关于效率——只要你没有数百万个字符串要解析,你就不需要担心。

关于python - 使用 Python re 的多模式嵌套正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60534183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com