我有这个字符串
procesor = "2x2.73 GHz Mongoose M5 & 2x2.50 GHz Cortex-A76 & 4x2.0 GHz Cortex-A55"通过使用re.findall(),我需要这个CPU核心列表。
Out:['2x2.73 GHz', '2x2.50 GHz', '4x2.0 GHz']请帮帮我。我被困在这里了:
re.findall('(\d+[A-Za-z])',procesor)
Out[1]: ['2x', '2x', '4x']发布于 2020-11-22 05:54:10
使用
re.findall(r'\d+x\d+(?:\.\d+)?\s*GHz', procesor)参见regex proof。
说明
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
x 'x'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
GHz 'GHz'如果您需要它不区分大小写:
re.findall(r'\d+x\d+(?:\.\d+)?\s*GHz', procesor, re.I)发布于 2020-11-22 07:13:21
在一种更便于人类阅读的格式中,[0-9]表示一个数字:
processor = "2x2.73 GHz Mongoose M5 & 2x2.50 GHz Cortex-A76 & 4x2.0 GHz Cortex-A55"
re.findall(r'[0-9]+x[0-9]+.[0-9]* GHz', processor)返回:
['2x2.73 GHz', '2x2.50 GHz', '4x2.0 GHz']发布于 2020-11-22 07:53:57
查看regex101中的示例!
将此代码附加到您的Python源代码中:
processor = """2x2.73 GHz Mongoose M5 & 2x2.50 GHz Cortex-A76 & 4x2.0 GHz Cortex-A55"""
CPU_Cores = re.findall("([\d.]+)\s?[xX]\s?([\d.]+)\s?GHz", processor)
print (CPU_Cores)输出
[('2', '2.73'), ('2', '2.50'), ('4', '2.0')]解释
([\d.]+)\s?[xX]\s?([\d.]+)\s?GHz
第一组real-number.
\s?[xX]\s?匹配第一组X.
\s?,([\d.]+)匹配x,x,x,X,X,第二组([\d.]+)是可选的,它匹配<x>D26或X.\s?。https://stackoverflow.com/questions/64948467
复制相似问题