1

I'm trying to parse a barely formated text to a price list. I store a bunch of regex patterns in a file looing like this:

[^S](7).*(\+|(plus)|➕).*(128)

When i attempt to verify whether there is a match like this:

def trMatch(line):
  for tr in trs:
    nr = re.compile(tr.nameReg, re.IGNORECASE)
    cr = re.compile(tr.colourReg, re.IGNORECASE)
    if (nr.search(line.text) is not None): doStuff()

I get an error

File "<stdin>", line 1, in <module>
  File "<stdin>", line 10, in go
  File "<stdin>", line 3, in trMatch
  File "/usr/lib/python3.5/re.py", line 224, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python3.5/re.py", line 292, in _compile
    raise TypeError("first argument must be string or compiled pattern")
TypeError: first argument must be string or compiled pattern

I assume it can't compile a pattern because it is missing 'r' flag. Is there a proper way to make this method to cooperate?

Thanks!

Pytoon
  • 11
  • 1

2 Answers2

0

The r"" syntax is not mandatory for working with regular expressions - this is just a helper syntax for escaping fewer characters, but it results in the same string. See What exactly do "u" and "r" string flags do, and what are raw string literals?

I'm not sure what trs is in your code, but it's a good guess that tr.nameReg and tr.colourReg are not strings: try to debug or print them and make sure they have the correct value.

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • all those attributes resolve as strings: >>> trs[9].nameReg '(✖|x|х).*(256)'. So i'll have to escape everything in the csv? – Pytoon Mar 04 '18 at 11:57
  • @Pytoon - I have a [working example here](https://tio.run/##K6gsycjPM/7/PzO3IL@oRKEolSsvMTc1KDVdwVZBXePRnGk1FTUXWzX1tDSMTM001bnyioASRal6yfm5BZk5qRpQ1TogMU93P/8gV2fHYFdNrsw0BY28Ir3i1MSi5AwN9QqgbnVNhcxihbz8EgW//LxUTSuFgqLMvBINdX9vRXXN//8B) with very similar code to yours. Can you post a complete example in the question, including how you initialize `trs`? I'm not sure what you mean by "escape", but it already looks like a valid regex pattern. – Kobi Mar 04 '18 at 13:33
0

Turns out re.search doesn't omit null patterns as I assumed. I added a simple check if there's a valid pattern and string to look in. Works like charm

Pytoon
  • 11
  • 1