-1

I have an ascii file which contains lines beginning with

^[[1m

that I would like to skip. I tried:

import re
f = open('MyFile.txt', 'r')
for line in f.readlines():
    if re.match(r'^[[1m',line):  
         continue

But I get an error message:

re_constants.error: unterminated character set at position 1

As suggested, I tried to use startswith but it doesn't match any line. Instead, escaping ^ and [ characters in the regex works. I had misunderstood that using the raw option r would have make escaping social characters unnecessary. Thx !

ciro
  • 1
  • 1

1 Answers1

2

The brackets are special char, and ^ too, you need to escape them to match the literal values

if re.match(r'\^\[\[1m', line):
azro
  • 53,056
  • 7
  • 34
  • 70
  • 1
    Note that the raw string literal `r'...'` is used to prevent Python from recognizing escape sequences as part of the string literal. It doesn't do anything to prevent the regular-expression engine the *reads* the string from treating its own metacharacters specially. – chepner Dec 29 '21 at 17:06
  • Thx, https://stackoverflow.com/users/1126841/chepner ! I completely missed this point ! – ciro Dec 29 '21 at 17:29
  • @ciro to tag someone do `@chepner` in your message :) – azro Dec 29 '21 at 17:31
  • @azro thx ! I was just looking hot to tag somebody ! – ciro Dec 29 '21 at 17:34