我试图用python编写regex语句,但很难同时捕获“<;<;”和“«”。 下面的正则表达式是我尝试过的,但是它没有捕获到我想要的内容。你知道吗
regex = "(<<)?«?{\w+}»?(>>)?(?=(\?|,|.|\s))"
使用regex
我尝试捕获3种类型的字符串。你知道吗
sent1 = "Do you want to eat «{Food}»? %[Y](A:y) %[N](A:n)"
sent2 = "You were drinking <<{coldBeverage}>>, do you want to drink <<{hotBeverage}>> instead?"
sent3 = "I am a {animal} who can talk."
我希望我可以按以下方式运行regex:
re.findall(regex, sent1) = ["«{Food}»"]
re.findall(regex, sent2) = ["<<{coldBeverage}>>", "<<{hotBeverage}>>"]
re.findall(regex, sent3) = ["{animal}"]
如果我们的示例仅限于列出的示例,我们可以从以下表达式开始:
用
测试re.finditer
用
测试re.findall
表达式在this demo的右上角面板上解释,如果您希望探索/简化/修改它,在this link中,如果您愿意,您可以一步一步地观察它如何与一些示例输入匹配。你知道吗
相关问题 更多 >
编程相关推荐