Python中使用re.search时出错:raise error, v # invalid expression, sre_constants.error: syntax error_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1900 | 回复: 0   主题: Python中使用re.search时出错:raise error, v # invalid expression, sre_constants.error: syntax error        下一篇 
    本主题由 koei123 于 2015-6-1 14:53:32 移动
谁是天蝎
注册用户
等级:大元帅
经验:90210
发帖:106
精华:0
注册:2011-7-21
状态:离线
发送短消息息给谁是天蝎 加好友    发送短消息息给谁是天蝎 发消息
发表于: IP:您无权察看 2015-4-9 15:57:21 | [全部帖] [楼主帖] 楼主

【问题】
python中,使用正则期间,用如下代码:

#http://autoexplosion.com/cars/buy/150594.php
foundMainType = re.search("http://autoexplosion\.com/(?<mainType>\w+)/buy/(?<adId>\d+)\.php", itemLink);


结果出错:

Traceback (most recent call last):
File "E:\Dev_Root\freelance\Elance\projects\40377988_data_mining\40377988_data_mining\40377988_data_mining.py", line 3
80, in <module>
main();
File "E:\Dev_Root\freelance\Elance\projects\40377988_data_mining\40377988_data_mining\40377988_data_mining.py", line 3
04, in main
itemInfoDict = processEachItem(itemLink);
File "E:\Dev_Root\freelance\Elance\projects\40377988_data_mining\40377988_data_mining\40377988_data_mining.py", line 1
83, in processEachItem
foundMainType = re.search("http://autoexplosion\.com/(?<mainType>\w+)/buy/(?<adId>\d+)\.php", itemLink);
File "E:\dev_install_root\Python27\lib\re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "E:\dev_install_root\Python27\lib\re.py", line 244, in _compile
raise error, v # invalid expression
sre_constants.error: syntax error


【解决过程】

1.调试了半天,结果也还是没找到错误的原因。

2.后来去看了re的语法,才发现是:

(?P<name>...)
Similar to regular parentheses, but the substring matched by the group is accessible within the rest of the regular expression via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named. So the group named id in the example below can also be referenced as the numbered group 1.
For example, if the pattern is (?P<id>[a-zA-Z_]\w*), the group can be referenced by its name in arguments to methods of match objects, such as m.group('id') or m.end('id'), and also by name in the regular expression itself (using (?P=id)) and replacement text given to.sub() (using \g<id>).


即,是:

(?P<xxx>…)


而不是:

(?<xxx>…)


所以,改为:

#http://autoexplosion.com/cars/buy/150594.php
foundMainType = re.search("http://autoexplosion\.com/(?P<mainType>\w+)/buy/(?P<adId>\d+)\.php", itemLink);


就可以了。

注:C#中的正则,named group是(?<xxx>…)

--转自 北京联动北方科技有限公司

该贴由koei123转至本版2015-6-1 14:53:31



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论