python - Python findall() 开始数字和结束词
问题描述
我有这个字符串
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']
解决方案
利用
re.findall(r'\d+x\d+(?:\.\d+)?\s*GHz', procesor)
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
\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)
推荐阅读
- spring-boot - 如何在 Spring Boot 应用程序中跟踪传出请求?
- oracle - 在 Oracle 云免费层中将 VM 与自治数据库连接
- javascript - 为什么在通过 HTTP 服务时需要指定 MIME 类型?
- python - 来自不同方法的python字典导致“缺少位置参数”错误
- android - 预期为 BEGIN_ARRAY,但在 GitHub API 中的第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT
- r - 如何编写自定义函数以从 `effects::Effect()` 中提取预测
- python - 尝试导入自己的包时出现 ModuleNotFoundError
- php - 在购物车 woocommerce 上同步两个产品数量
- go - template.Execute 不传递数据
- python - 带有 TortoiseORM 的 FastAPI:错误:无法处理的实体