Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单问题的Python正则表达式_Python_Regex_Python 2.7 - Fatal编程技术网

简单问题的Python正则表达式

简单问题的Python正则表达式,python,regex,python-2.7,Python,Regex,Python 2.7,我希望让用户问一个简单的问题,这样我就可以从输入的字符串中提取一些标准元素 要输入的字符串示例: 谁是《黑暗骑士》的导演 中国的首都是什么 谁是美国总统 正如你所看到的,有时是“谁”,有时是“什么”。我很可能在找“|”操作符。我需要从这些字符串中提取两个内容。“The”之后和“of”之前的单词,以及“of”之后的单词 例如: 第一句话:我希望提取“director”并将其放入名为Relation的变量中,提取“黑暗骑士”并将其放入名为Concept的变量中 期望输出: RelationVar

我希望让用户问一个简单的问题,这样我就可以从输入的字符串中提取一些标准元素

要输入的字符串示例:

  • 谁是《黑暗骑士》的导演
  • 中国的首都是什么
  • 谁是美国总统
正如你所看到的,有时是“谁”,有时是“什么”。我很可能在找“|”操作符。我需要从这些字符串中提取两个内容。“The”之后和“of”之前的单词,以及“of”之后的单词

例如:

第一句话:我希望提取
“director”
并将其放入名为
Relation
的变量中,提取
“黑暗骑士”
并将其放入名为
Concept
的变量中

期望输出:

RelationVar = "director"
ConceptVar = "The Dark Knight"
第二句话:我希望提取“资本”,将其分配给变量“关系”……并提取“中国”,并将其置于变量“概念”中


关于如何使用
re.match
功能有什么想法吗?或者任何其他方法?

您想为谁/什么使用
是正确的。正则表达式的其余部分非常简单,为了清晰起见,这里有组名,但是您可以使用
r”(?:Who | What)代替

>>> r = r"(?:Who|What) is the (?P<RelationVar>.+) of (?P<ConceptVar>.+)[?]"
>>> l = ['Who is the director of The Dark Knight?', 'What is the capital of China?', 'Who is the president of USA?']
>>> [re.match(r, i).groupdict() for i in l]
[{'RelationVar': 'director', 'ConceptVar': 'The Dark Knight'}, {'RelationVar': 'capital', 'ConceptVar': 'China'}, {'RelationVar': 'president', 'ConceptVar': 'USA'}]

这是脚本,您可以简单地使用|在括号内选择匹配一个

这对我很管用

import re
list = ['Who is the director of The Dark Knight?','What is the capital of China?','Who is the president of USA?']
for string in list:
    a = re.compile(r'(What|Who) is the (.+) of (.+)')
    nodes = a.findall(string);
    Relation = nodes[0][0]
    Concept = nodes[0][1]
    print Relation
    print Concept
    print '----'

最好的祝愿:)

自然语言解析并不简单。你可以用正则表达式把句子分成几个单词,但这就是你所能做的。所有的问题都是
的形式吗?
@Barmar是的,所有的问题都是以这种形式来保持简单:)在这种情况下,正则表达式应该非常简单,你尝试了什么,它是如何失败的?@Barmar tbh我没有尝试太多,因为我不能完全理解这个概念。我现在正在看python正则表达式上的谷歌视频,看看我是否能理解它。我一直关注的部分是如何提取并将其分配给变量(希望视频能够解释这一点),这确实是我所追求的。问题:现在,您已经将问题硬编码到其中作为示例(显然)…因此,现在我将尝试使用用户输入来分配变量“l”。正确的?Thanx:)@RHK-S8如果您想获得用户输入,请查看
raw_input
函数(或者如果您使用的是python 3,只需
input
)。很高兴为您提供帮助:)顺便问一下,这是否意味着在您的示例中它现在已分配了键/值对?因为我不知道他们是如何分配的:(很抱歉问了一些愚蠢的问题。啊,没有刷新页面,我的糟糕
>>> m = re.match(r, "What is the capital of China?")
>>> d = m.groupdict()
>>> relation_var = d["RelationVar"]
>>> concept_var = d["ConceptVar"]
>>> relation_var
'capital'
>>> concept_var
'China'
import re
list = ['Who is the director of The Dark Knight?','What is the capital of China?','Who is the president of USA?']
for string in list:
    a = re.compile(r'(What|Who) is the (.+) of (.+)')
    nodes = a.findall(string);
    Relation = nodes[0][0]
    Concept = nodes[0][1]
    print Relation
    print Concept
    print '----'