problem with regex

A

abcd

I have a regex: '[A-Za-z]:\\([^/:\*\?"<>\|])*'

when I do, re.compile('[A-Za-z]:\\([^/:\*\?"<>\|])*') ...I get

sre_constants.error: unbalanced parenthesis

do i need to escape something else? i see that i have matching
parenthesis.

thx
 
R

Rob Wolfe

abcd said:
I have a regex: '[A-Za-z]:\\([^/:\*\?"<>\|])*'

when I do, re.compile('[A-Za-z]:\\([^/:\*\?"<>\|])*') ...I get

sre_constants.error: unbalanced parenthesis

do i need to escape something else? i see that i have matching
parenthesis.

You should use raw string:

re.compile(r'[A-Za-z]:\\([^/:\*\?"<>\|])*')

Regards,
Rob
 
B

Barry

I have a regex: '[A-Za-z]:\\([^/:\*\?"<>\|])*'

when I do, re.compile('[A-Za-z]:\\([^/:\*\?"<>\|])*') ...I get

sre_constants.error: unbalanced parenthesis

do i need to escape something else? i see that i have matching
parenthesis.

thx

--

Try making the argument a raw string:
re.compile(r'[A-Za-z]:\\([^/:\*\?"<>\|])*')
 
T

Tim Chase

sre_constants.error: unbalanced parenthesis


Because you're not using raw strings, the escapables become
escaped, making your regexp something like

[A-Za-z]:\([^/:\*\?"<>\|])*

(because it knows what "\\" is, but likely doesn't attribute
significance to "\?" or "\|", and thus leaves them alone).

Thus, you have "\(" in your regexp, which is a literal
open-paren. But you have a ")", which is a "close a grouping"
paren. The error is indicating that the "close a grouping" paren
doesn't close some previously opened paren.

General good practice shoves all this stuff in a raw string:

r"[A-Za-z]:\\([^/:\*\?"<>\|])*"

which solves much of the headache.

-tkc
 
A

abcd

well thanks for the quick replies, but now my regex doesn't work.

Code:
import re
p = re.compile(r'[A-Za-z]:\\([^/:\*?"<>\|])*')

x = p.match("c:\test")

x is None

any ideas why? i escape the back-slash, the asterisk *, and the PIPE |
.....b/c they are regex special characters.
 
S

Sybren Stuvel

abcd enlightened us with:
well thanks for the quick replies, but now my regex doesn't work.

Code:
import re
p = re.compile(r'[A-Za-z]:\\([^/:\*?"<>\|])*')

x = p.match("c:\test")

x is None

any ideas why?

Yes, because after the "c:" you expect a backslash, and not a tab
character. Read the manual again about raw strings and character
escaping, it'll do you good.

Sybren
 
A

abcd

sorry i forgot to escape the question mark...
Code:
import re
p = re.compile(r'[A-Za-z]:\\([^/:\*?"<>\|])*')[/QUOTE]

even when I escape that it still doesnt work as expected.

p = re.compile(r'[A-Za-z]:\\([^/:\*\?"<>\|])*')

p.match('c:\test')  still returns None.
 
T

Tim Chase

p = re.compile(r'[A-Za-z]:\\([^/:\*? said:
x = p.match("c:\test")
any ideas why? i escape the back-slash, the asterisk *, and the PIPE |
....b/c they are regex special characters.


Same problem, only now in the other string:
c: est

Your "\t" is interpreted as as tab character. Thus, you want

s = r"c:\test"

or

s = "c:\\test"

which you'll find should now be successfully found with

p.match(s)

-tkc
 
A

abcd

Sybren said:
Yes, because after the "c:" you expect a backslash, and not a tab
character. Read the manual again about raw strings and character
escaping, it'll do you good.


doh. i shall do that.

thanks.
 
T

Tim Chase

regex = r'[A-Za-z]:\\([^/:\*\? said:
<_sre.SRE_Match object at 0x009D7720>

the last example shouldnt give a match

Ah, but it should, because it *does* match.
>>> m = p.match('c:\\test?:/')
>>> m.group(0) 'c:\\test'
>>> # add a "$" at the end to anchor it
>>> # to the end of the line
>>> regex = r'[A-Za-z]:\\([^/:\*\?"<>\|])*$'
>>> p = re.compile(regex)
>>> m = p.match('c:\\test?:/')
>>> m

By adding the "$" to ensure that you're matching the whole string
passed to match() and not just as much as possible given the
regexp, you solve the problem you describe.

-tkc
 
R

Rob Wolfe

abcd said:
not sure why this passes:

regex = r'[A-Za-z]:\\([^/:\*\?"<>\|])*'
p = re.compile(regex)
p.match('c:\\test')

the last example shouldnt give a match

If you want to learn RE I suggest to use great tool redemo.py (tk app).
Then you can play with regular expressions to find the result
you are looking for.
It can be found in Python 2.4 in Tools\Scripts.

Regards,
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top