Nov-07-2017, 06:37 PM
Hello everyone,
First I'm a beginner at Python and I'm trying to learn by testing a few different thing and now I'm stuck. First of all I use this text file ss100.txt. It's in Swedish so there is of course åäö in the file and that's a problem as well. My question is as follows:
How do I find all words with a pattern like this: hxxxgxx where x is an unknown character?
I tried this but it's not working and I ran out of ideas:
#!/usr/bin/env python # -*- coding: utf-8 -*- import re thefile = open("ss100.txt", "r") for line in thefile: if re.match("h(.*)g(.*)(.*)", line) and len(line)==7: print line
A small example of the text file in the link above:
Quote:hopplös
hopplösa
hopplösare
hopplösares
hopplösas
hopplösast
hopplösaste
hopplösastes
hopplöse
hopplöses
hopplöshet
hopplösheten
hopplöshetens
hopplöshets
hopplöst
I'm looking for words with 7 characters starting with 'h' and a 'g' in position 5.
What is wrong with my code? Or is there a better way to do this.