Python: Regex – Matching Foreign Characters/Unicode Letters
In the world of regular expressions, matching characters outside of the usual Latin character set can be a challenge. Read on to find out how author Mark Needham tackled this issue in Python.
Join the DZone community and get the full member experience.
Join For FreeI’ve been back in the land of screen scraping this week extracting data from the Game of Thrones wiki and needed to write a regular expression to pull out characters and actors.
Here are some examples of the format of the data:
1 Peter Dinklage as Tyrion Lannister 2 Daniel Naprous as Oznak zo Pahl(credited as Stunt Performer) 3 Filip Lozić as Young Nobleman 4 Morgan C. Jones as a Braavosi captain 5 Adewale Akinnuoye-Agbaje as Malko |
So the pattern is:
<actor> as <character>
Optionally followed by some other text that we’re not interested in.
The output I want to get is:
5 1 Peter Dinklage, Tyrion Lannister 2 Daniel Naprous, Oznak zo Pahl 3 Filip Lozić, Young Nobleman 4 Morgan C. Jones, a Braavosi captain 5 Adewale Akinnuoye-Agbaje, Malko |
I started using the ‘split’ command on the word ‘as’ but that broke down when I realized some of the characters had the letters ‘as’ in the middle of their name. So, regex it is!
This was my first attempt:
20 1 import re 2 3 strings = [ 4 "Peter Dinklage as Tyrion Lannister", 5 "Filip Lozić as Young Nobleman", 6 "Daniel Naprous as Oznak zo Pahl(credited as Stunt Performer)", 7 "Morgan C. Jones as a Braavosi captain", 8 "Adewale Akinnuoye-Agbaje as Malko" 9 ] 10 11 regex = "([A-Za-z\-'\. ]*) as ([A-Za-z\-'\. ]*)" 12 13 for string in strings: 14 print string 15 match = re.match( regex, string) 16 if match is not None: 17 print match.groups() 18 else: 19 print "FAIL" 20 print "" |
x 1 Peter Dinklage as Tyrion Lannister 2 ('Peter Dinklage', 'Tyrion Lannister') 3 4 Filip Lozić as Young Nobleman 5 FAIL 6 7 Daniel Naprous as Oznak zo Pahl(credited as Stunt Performer) 8 ('Daniel Naprous', 'Oznak zo Pahl') 9 10 Morgan C. Jones as a Braavosi captain 11 ('Morgan C. Jones', 'a Braavosi captain') 12 13 Adewale Akinnuoye-Agbaje as Malko 14 ('Adewale Akinnuoye-Agbaje', 'Malko') |
It works for 4 of the 5 scenarios but not for Filip Lozić. The ‘ć’ character causes the issue so we need to be able to match foreign characters which the current charset I defined in the regex doesn’t capture.
I came across this Stack Overflow post which said that in some regex libraries you can use ‘\p{L}’ to match all letters. I gave that a try:
1 1 regex = "([\p{L}\-'\. ]*) as ([\p{L}\-'\. ]*)" |
And then re-ran the script:
14 1 Peter Dinklage as Tyrion Lannister 2 FAIL 3 4 Daniel Naprous as Oznak zo Pahl(credited as Stunt Performer) 5 FAIL 6 7 Filip Lozić as Young Nobleman 8 FAIL 9 10 Morgan C. Jones as a Braavosi captain 11 FAIL 12 13 Adewale Akinnuoye-Agbaje as Malko 14 FAIL |
Hmmm, not sure if I did it wrong or if that isn’t available in Python. I’ll assume the latter but feel free to correct me in the comments and I’ll update the post.
I went search again and found this post which suggested another approach:
You can construct a new character class:
[^\W\d_]
instead of \w. Translated into English, it means “Any character that is not a non-alphanumeric character ([^\W] is the same as \w), but that is also not a digit and not an underscore”.
Let’s try plugging that in:
1 1 regex = "([A-Za-z\-'\.^\W\d_ ]*) as ([A-Za-z\-'\.^\W\d_ ]*)" |
14 1 Peter Dinklage as Tyrion Lannister 2 ('Peter Dinklage', 'Tyrion Lannister') 3 4 Daniel Naprous as Oznak zo Pahl(credited as Stunt Performer) 5 ('Daniel Naprous as Oznak zo Pahl(credited', 'Stunt Performer)') 6 7 Filip Lozić as Young Nobleman 8 ('Filip Lozi\xc4\x87', 'Young Nobleman') 9 10 Morgan C. Jones as a Braavosi captain 11 ('Morgan C. Jones', 'a Braavosi captain') 12 13 Adewale Akinnuoye-Agbaje as Malko 14 ('Adewale Akinnuoye-Agbaje', 'Malko') |
So that’s fixed Filip but now Daniel Naprous is being incorrectly parsed.
For Attempt #4 I decided to try excluding what I don’t want instead:
1 1 regex = "([^0-9\(]*) as ([^0-9\(]*)" |
14 1 Peter Dinklage as Tyrion Lannister 2 ('Peter Dinklage', 'Tyrion Lannister') 3 4 Daniel Naprous as Oznak zo Pahl(credited as Stunt Performer) 5 ('Daniel Naprous', 'Oznak zo Pahl') 6 7 Filip Lozić as Young Nobleman 8 ('Filip Lozi\xc4\x87', 'Young Nobleman') 9 10 Morgan C. Jones as a Braavosi captain 11 ('Morgan C. Jones', 'a Braavosi captain') 12 13 Adewale Akinnuoye-Agbaje as Malko 14 ('Adewale Akinnuoye-Agbaje', 'Malko') |
That does the job but has exposed my lack of regex skills. If you know a better way let me know in the comments.
Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments