问题描述
我看到了许多示例,这些示例说明了如何在正则表达式之间为python获取括号之间的数据,但没有一个内部带有某种模式。
例如,我有以下数据:
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)
我想至少实现的目标是:
(94.6 × 189.2 × 20 dm)
(76.2 x 127 dm.)
(61.9 x 37.1 x 13 dm)
完美的结果将是下面的结果,但是我确信这将需要第二次拆分:
94.6,189.2,20
76.2,127
61.9,37.1,13
当前,我正在尝试以下代码:regex,但是正如您所见,仅捕获cm括号数据并没有成功。
解决方法
使用
\(([^()]*\bcm\b[^()]*)\)
请参见proof
说明
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
( 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
--------------------------------------------------------------------------------
\) ')'