简体   繁体   English

正则表达式,用于匹配括号之间的数据,但内部有一个模式匹配

[英]Regex for matching data between parenthesis BUT with a pattern to match inside

I saw many examples of how to get data between parenthesis with regex for python but none with some pattern inside.我看到了许多示例,说明如何使用正则表达式为 python 获取括号之间的数据,但没有一个包含某种模式的示例。

For example, I have this data:例如,我有以下数据:

Overall (each): 37 1/4 × 74 1/2 × 7 7/8 in. (94.6 × 189.2 × 20 dm)
Each, 30 x 50 in. (76.2 x 127 dm.)
24 3/8 x 14 5/8 x 5 1/8 in. (61.9 x 37.1 x 13 dm)

What I am tryng to achieve at least is:我至少要实现的是:

(94.6 × 189.2 × 20 dm)
(76.2 x 127 dm.)
(61.9 x 37.1 x 13 dm)

And the perfect result would be what is below but I am sure this will require a second split:完美的结果将是以下内容,但我确信这将需要第二次拆分:

94.6, 189.2, 20 
76.2, 127
61.9, 37.1, 13

Currently, I am trying this code: regex , but as you can see without the success in capturing just the cm parenthesis data.目前,我正在尝试以下代码: regex ,但正如您所看到的,仅捕获 cm 括号数据没有成功。

Use

\(([^()]*\bcm\b[^()]*)\)

See proof查看证明

Explanation解释

--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^()]*                   any character except: '(', ')' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    cm                       'cm'
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    [^()]*                   any character except: '(', ')' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \)                       ')'

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

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