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

我看到了很多关于如何使用 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 括号数据。


qq_遁去的一_1
浏览 52回答 1
1回答

慕运维8079593

使用\(([^()]*\bcm\b[^()]*)\)查看证明解释--------------------------------------------------------------------------------  \(                       '('--------------------------------------------------------------------------------  (                        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--------------------------------------------------------------------------------  \)                       ')'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python