Skip to main content
 首页 » 编程设计

python之python : try to detect `column` datatype(digits) 中的正则表达式问题

2025年05月04日28txw1958

我想区分像

这样的字符串
`citizen_group` int(10) NOT NULL, 
`container_group` int(10) NOT NULL, 

来自像这样的字符串

PRIMARY KEY (`citizen_group`,`container_group`), 
KEY `fk_containergroup_readeraccess` (`container_group`) 

因此我创建了这个正则表达式

`?\w+`?\s\w+\(d+(,\s?\d+)?\) 
   ^ 
 
*\w+`*   : string, wrapped in ` is possible.                `column` 
\s       : followed by a space                        
\w+      : followed by one or more characters                real 
\(d+     : followd by ( with one or more digits              (10 
(,\s?\d)?: followed by 0 or 1 , with digits                  ,3 
\)       : end with a )                                      ) 

但是这个正则表达式不仅选择前 2 个字符串,还选择最后一个

KEY `fk_containergroup_readeraccess` (`container_group`) 

谁能告诉我这是为什么?我需要如何修改正则表达式以使其仅选择前两个?

请您参考如下方法:

我用这个程序测试过:

import re, sys 
for line in sys.stdin: 
    match = re.search(r'`?\w+`?\s\w+\(\d+(,\s?\d+)?\)', line) 
    if match: 
        print match.group(0) 

(请注意,您在“\d”中紧跟在左括号后面的位置缺少“\”;我在此测试中修复了该问题。)

它与您的前两个示例匹配,但与您的后两个示例不匹配,因此我相信它按您的预期工作。