-4

I have a list that I want to loop through (for trimtemp in trim) and see if it has any matches in a given string (modeltrim). The code runs fine most of the time however, every now and then it gives me the following error:

Traceback (most recent call last):
  File "/home/hostadl/PricesFinal.py", line 318, in <module>
    main()
  File "/home/hostadl/PricesFinal.py", line 215, in main
    match = re.search(r'{0}'.format(trimtemp), modeltrim, re.IGNORECASE)
  File "/usr/local/lib/python3.2/re.py", line 158, in search
    return _compile(pattern, flags).search(string)
  File "/usr/local/lib/python3.2/re.py", line 256, in _compile
    return _compile_typed(type(pattern), pattern, flags)
  File "/usr/local/lib/python3.2/functools.py", line 180, in wrapper
    result = user_function(*args, **kwds)
  File "/usr/local/lib/python3.2/re.py", line 268, in _compile_typed
    return sre_compile.compile(pattern, flags)
  File "/usr/local/lib/python3.2/sre_compile.py", line 491, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/local/lib/python3.2/sre_parse.py", line 692, in parse
    p = _parse_sub(source, pattern, 0)
  File "/usr/local/lib/python3.2/sre_parse.py", line 315, in _parse_sub
    itemsappend(_parse(source, state))
  File "/usr/local/lib/python3.2/sre_parse.py", line 642, in _parse
    raise error("unbalanced parenthesis")
sre_constants.error: unbalanced parenthesis

Your help would be greatly appreciated!

jordanskis
  • 257
  • 1
  • 4
  • 11
  • 7
    Please also post the code which errors out. We can pick out one relevant line from the trace, but need to see more of it. – Michael Berkowski Jun 28 '12 at 02:27
  • 6
    Without knowing what is in `trimtemp`, we can't tell you any more than the error already does. The error would appear to be self explanatory though. – Hamish Jun 28 '12 at 02:30

1 Answers1

2

This line appears to be the problem

match = re.search(r'{0}'.format(trimtemp), modeltrim, re.IGNORECASE)

You should look at what format(trimtemp) contains. Perhaps add just add a

print format(trimtemp)

on the line before. Probably it is containing ( or ) which are of significance to re. Perhaps re.escape(format(trimtemp)) is what you need

John La Rooy
  • 295,403
  • 53
  • 369
  • 502