简体   繁体   English

带引号的正则表达式模式失败

[英]Regex pattern with quotation marks fails

I am a bit puzzled with the failure of this search:我对这次搜索失败感到有些困惑:

epwnymia_string = ' «Β. ΚΙΟΜΟΥΡΤΖΙΔΗΣ – Χ. ΚΙΟΜΟΥΡΤΖΙΔΗΣ ΟΕ» Ο ΠΡΟΕΔΡΟΣ Την 25/9/2013 καταχωρήθηκε με ΚΑΚ 100862 στο Γε'
epwnymia_pattern = re.compile(r'«[^«»]»')
epwnymia_pattern.search(epwnymia_string) # fails to match Β. ΚΙΟΜΟΥΡΤΖΙΔΗΣ – Χ. ΚΙΟΜΟΥΡΤΖΙΔΗΣ ΟΕ

What causes the failure and how should I correct my code?是什么导致了失败,我应该如何更正我的代码?

You can do either + or * to get all the characters in-between « » and put the [^«»]* part in brackets so you will be able to get the inner value.您可以使用 + 或 * 来获取 « » 之间的所有字符,并将 [^«»]* 部分放在括号中,这样您就可以获得内部值。

import re

epwnymia_string = ' «Β. ΚΙΟΜΟΥΡΤΖΙΔΗΣ – Χ. ΚΙΟΜΟΥΡΤΖΙΔΗΣ ΟΕ» Ο ΠΡΟΕΔΡΟΣ Την 25/9/2013 καταχωρήθηκε με ΚΑΚ 100862 στο Γε'
epwnymia_pattern = re.compile(r'«([^«»]*)»')
print(epwnymia_pattern.search(epwnymia_string).group(1))

Output Output

Β. ΚΙΟΜΟΥΡΤΖΙΔΗΣ – Χ. ΚΙΟΜΟΥΡΤΖΙΔΗΣ ΟΕ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM