首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个正则表达式不起作用?它应该是基于diveintopython3工作的

为什么这个正则表达式不起作用?它应该是基于diveintopython3工作的
EN

Stack Overflow用户
提问于 2016-08-12 02:34:29
回答 1查看 30关注 0票数 1
代码语言:javascript
复制
pattern='''

^                     #beginning of string

M{0,3}              # thousands- 0 to 3 MS

(CM|CD|D?C{0,3})    # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 Cs), or 500-800 (D, followed by 0 to 3 Cs)

(XC|XL|L?X{0,3})    # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 Xs), or 50-80 (L, followed by 0 to 3 Xs)

(IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 Is), or 5-8 (V, followed by 0 to 3 Is)

$                   #end of string
'''

根据diveintopython3网站和我的逻辑,re.search(pattern,'M',re.VERBOSE)应该返回字符串匹配,但当我进入call re.search时没有返回。为什么会这样呢?

EN

回答 1

Stack Overflow用户

发布于 2016-08-12 02:40:32

它对我来说很好:

代码语言:javascript
复制
romanstring = "M"
m = re.search(pattern, romanstring, re.VERBOSE)
print(m.string)

也许您感到困惑,因为re.search返回一个“匹配对象”,而不是直接返回匹配的字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38903795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档