user8270077
user8270077

Reputation: 5071

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?

Upvotes: 0

Views: 34

Answers (1)

Leo Arad
Leo Arad

Reputation: 4472

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

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

Upvotes: 1

Related Questions