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

最近在用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[-+])?              # an optional sign, followed by either...

(

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

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

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

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

|

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

|

(?Ps)?           # ...an (optionally signaling)

NaN                      # NaN

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

)

#    /s*

/Z

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

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

......

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

""", 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有关,只能区分大小写。

至此这个异常终于解决了,记录下来以备查看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值