您的位置:首页 > 编程语言 > Python开发

关于Python正则表达式的区分大小写的问题

2010-05-23 23:36 681 查看
最近在用Python处理一些数据,数据需要存储到MySQL数据库中,采用MySQLdb来进行数据库的操作,但是被一个问题困扰了很久。在打开数据库的时候MySQLdb.connect(self.host, self.user, self.password, self.database, port=self.port)出异常,而且异常出现的位置非常奇怪。

出现在converters.py 164行

from decimal import Decimal

进去之后发现出错的位置在decimal.py文件中5833行出错

import re

_parser = re.compile(r"""        # A numeric string consists of:

#    /s*

    (?P<sign>[-+])?              # an optional sign, followed by either...

    (

        (?=/d|/./d)              # ...a number (with at least one digit)

        (?P<int>/d*)             # having a (possibly empty) integer part

        (/.(?P<frac>/d*))?       # followed by an optional fractional part

        (E(?P<exp>[-+]?/d+))?    # followed by an optional exponent, or...

    |

        Inf(inity)?              # ...an infinity, or...

    |

        (?P<signal>s)?           # ...an (optionally signaling)

        NaN                      # NaN

        (?P<diag>/d*)            # with (possibly empty) diagnostic info.

    )

#    /s*

    /Z

""", re.VERBOSE | re.IGNORECASE | re.UNICODE).match

在编译正则表达式的时候出错,错误信息为

......

  File "D:/Python27/lib/decimal.py", line 5850, in <module>

    """, re.VERBOSE | re.IGNORECASE | re.UNICODE).match

  File "D:/Python27/lib/re.py", line 190, in compile

    return _compile(pattern, flags)

  File "D:/Python27/lib/re.py", line 245, in _compile

    raise error, v # invalid expression

sre_constants.error: internal: unsupported template operator

居然是系统库文件出错!

 

但我在其他地方调用MySQLdb.connect能够正常连接数据库,这就奇怪了,库文件的代码如果有问题应该总出错才对,所以排除了库文件代码的问题。

 

仔细对比了一下代码,没有发现有什么不同,连接过程都是一样的。

这个错误执行到编译正则表达式这一行应该和re模块有关系,仔细一想,我在代码里设置了re.IGNORECASE = True用来匹配的时候不区分大小写,将这一行注释掉之后在运行,果然异常消失了。看来这个东西还和IGNORECASE有关,只能区分大小写。

 

至此这个异常终于解决了,记录下来以备查看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息