Forums

error: "missing ), unterminated subpattern"

2020-11-05 04:43:11,196: File "/usr/lib/python3.8/sre_parse.py", line 443, in _parse_sub

2020-11-05 04:43:11,196: itemsappend(_parse(source, state, verbose, nested + 1,

2020-11-05 04:43:11,197:

2020-11-05 04:43:11,197: File "/usr/lib/python3.8/sre_parse.py", line 836, in _parse

2020-11-05 04:43:11,197: raise source.error("missing ), unterminated subpattern",

Can anyone help me with this? I suddenly started getting this strange error out of nowhere.

Have you switched a version of python or a version of one of your installed packages?

No, I haven't.

The weird thing is that there is similar code all throughout my script, but they don't raise any errors, and when I comment the problematic code out, the program runs fine.

I am completely stumped by this at the moment as I'm still getting the error even when I copy + paste code and tweak some of the variables. It may be an specific issue with the regex pattern I'm parsing but even then I don't see where I might be going wrong.

It does sound like there's an error with the specific regex, yes. Is it something you can share here?

I'll show just the regex expression for now, but if you guys need me to share the rest of the code, I'd be happy to oblige. The project I'm working on is purely for my own learning, after all.

So I'm developing a chatbot that listens for various callbacks and launches functions based on what it receives.

The regex in question looks like this:

pattern='^' + str(TO_WIKI_MENU) + '$'

So it only listens for this specific string and launches a certain command. It goes through things like re.compile and sre_parse.parse on its own.

Like I mentioned, other patterns like:

pattern='^' + str(PHOTO_1_BLUR) + '$'

pattern='^' + str(PHOTO_2_ROTATE_180) + '$'

aren't causing any issues and the chatbot runs fine with them

It sounds like your TO_WIKI_MENU variable contains text that is being interpreted as special characters by the regex parser -- what value does it have?

I used map(chr, range) there might be something going on with that, for example:

(TO_WIKI_MENU, WIKIPEDIA_START, RETURN_TO_WIKI_MENU) = map(chr, range(40,43))

I have managed to fix the problem, and I know the cause.

By using map(chr, range), I was assigning Unicode characters to each variable. So the variable 'TO_WIKI_MENU' was assigned the value of chr(40), which in this case is '('.

So.. when I tried re.parse(pattern), I wasn't parsing the string '^TO_WIKI_MENU$', like I thought. I was parsing '^($' which is what's causing the error.

So I managed to fix the problem by just using: (variables, variables, variables...) = range(x, y). The chatbot works fine now.

Thanks for the quick responses, guys. the last 2 posts made it click in my mind.

Thanks for letting us know!

Hello, I seem to have the same error:

File "/home/...., line 379, in findWholeWord return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search File "/usr/local/lib/python3.9/re.py", line 252, in compile return _compile(pattern, flags) File "/usr/local/lib/python3.9/re.py", line 304, in _compile p = sre_compile.compile(pattern, flags) File "/usr/local/lib/python3.9/sre_compile.py", line 764, in compile p = sre_parse.parse(p, flags) File "/usr/local/lib/python3.9/sre_parse.py", line 948, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) File "/usr/local/lib/python3.9/sre_parse.py", line 443, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, File "/usr/local/lib/python3.9/sre_parse.py", line 805, in _parse flags = _parse_flags(source, state, char) File "/usr/local/lib/python3.9/sre_parse.py", line 887, in _parse_flags raise source.error(msg, len(char)) re.error: missing -, : or ) at position 5

with this code: def findWholeWord(w): return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search

The above code works well with my python idle (version 3.9) but after changing to version 3.9 on PA, this erros comes on board. ANy help?

The regex you're trying to compile includes the contents of the variable w, so my guess would be that there's a value that w can take that is breaking the regex. try to see if a particular value of w is the problem.