python 正则表达式 实例

其中
.+ 代表匹配任意字符1次到无限次
\s 代表匹配空格、制表符、换页符等等,等价于 [\f\n\r\t\v]

import re

target_pattern = ["( lambda ?x ( P E ?x ) )",
"( lambda ?x ( and ( P E ?x ) ( P E ?x ) ) )",
"( lambda ?x Exist ?y ( and ( P E ?y ) ( P ?y ?x ) ) )",
"( lambda ?x Exist ?y ( and ( P E ?y ) ( P ?y E ) ( P ?y ?x ) ) )",
"( lambda ?x Exist ?y ( and ( P E ?y ) ( P ?y ?x ) ( P ?y ?x ) ) )",
]

patterns = []
pa = re.compile('lambda.+x\s.+mso.+x')
patterns.append(pa)

pa = re.compile('lambda.+x\s.+and.+mso.+x.+mso.+x')
patterns.append(pa)

pa = re.compile('lambda.+x\sExist.+and.+mso.+y.+mso.+y.+x')
patterns.append(pa)

pa = re.compile('lambda.+x\sExist.+and.+mso.+y.+mso.+y.+mso.+y.+x')
patterns.append(pa)

pa = re.compile('lambda.+x\sExist.+and.+mso.+y.+mso.+y.+x.+mso.+y.+x')
patterns.append(pa)

print(patterns[0].match("lambda ?x ( mso:meteorology.tropical_cyclone.total_fatalities Cyclone_Bejisa ?x )").group())

print(patterns[1].match("lambda ?x ( and ( r-mso:film.film.cinematographer Jost_Vacano ?x ) ( r-mso:film.film.story Ronald_Shusett ?x ) )").group())

print(patterns[2].match("lambda ?x Exist ?y ( and ( mso:film.character.film Radagast ?y ) ( mso:film.film.location ?y ?x ) )").group())

print(patterns[3].match("lambda ?x Exist ?y ( and ( mso:projects.project.project_participation SNRC_:_Robust_and_Adaptive_Protocol_Design_for_Multimedia_Wireless_Networks_research ?y ) ( mso:projects.project_participation.project_participant ?y Stanford_Networking_Research_Center ) ( mso:projects.project_participation.from_date ?y ?x ) ) )").group())

print(patterns[4].match("lambda ?x Exist ?y ( and ( mso:organization.organization.leadership derby_moor_community_sports_college ?y ) ( mso:organization.leadership.role ?y ?x ) ( mso:organization.leadership.person ?y ?x ) )").group())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FocusOneThread

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值