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

标签 python regex

我想区分像

这样的字符串
`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”中紧跟在左括号后面的位置缺少“\”;我在此测试中修复了该问题。)

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

关于python - python : try to detect `column` datatype(digits) 中的正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262022/

相关文章:

java - 如何在Java程序中通过正则表达式仅显示网页内容(不显示任何标签、链接)

python - flask SQLAlchemy : How to add a column that depends on another table?

php - 将没有分隔符的字符串转换为关联多维数组

python - python中使用正则表达式判断变量类型

基于 Pandas 时间序列级别创建 1 和 0 的递归序列的 Pythonic 方法

javascript - 仅使用 JavaScript 获取基本主机名,以便 Cookie 在整个站点范围内工作

java - 如何在java中使用正则表达式分割一个大数?

python - Django 有哪些 Pylons 没有的功能?

python - 我怎样才能允许只对部分模式进行模糊正则表达式匹配?

python - 将以逗号分隔的字符串的pandas列转换为虚拟变量