其中
.+
代表匹配任意字符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())