首页 > 解决方案 > 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']

标签: pythonregexfindall

解决方案


利用

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)

推荐阅读