pythonregexutf-8python-3.5

Python3 regular expression not working on script, but works on pythex.org


I am writing a script in Python3 and I want to use regular expressions. I have some utf-8 encoded files used as configuration files for my main script.

I wish to change some lines on them (classic configuration changes).

My code ,still in poc condition, is this:

regex = re.compile('^SHOW_ALL\s[^:]')
with open('./config.txt', encoding='utf-8', mode='r+') as old_file:
    for line in old_file.read():
            if regex.match(line):
                print(line)

and the config.txt is this:

#Κάτι στα ελληνικά

SHOW_ALL OFF 15
PRINT ON
SHOW_VALUES O

COM 0
PRINTER_NAME samsung_not_a_real_name
CAMERA 33

I checked my regular expression at pythex.org and it seems to work fine.

What could be going wrong?

*the link redirects to the exact regex and text I tried myself at regex.org


Solution

  • Try to replace old_file.read(); by old_file.readLines();