473,898 Members | 2,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with regex

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

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

sre_constants.e rror: unbalanced parenthesis

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


Jul 28 '06 #1
10 3400

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

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

sre_constants.e rror: unbalanced parenthesis

do i need to escape something else? i see that i have matching
You should use raw string:



Jul 28 '06 #2
On 28 Jul 2006 05:45:05 -0700, abcd <co*******@gmai l.comwrote:
I have a regex: '[A-Za-z]:\\([^/:\*\?"<>\|])*'

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

sre_constants.e rror: unbalanced parenthesis

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


Try making the argument a raw string:
Jul 28 '06 #3
when I do, re.compile('[A-Za-z]:\\([^/:\*\?"<>\|])*') ...I get
sre_constants.e rror: unbalanced parenthesis

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


(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:


which solves much of the headache.


Jul 28 '06 #4
well thanks for the quick replies, but now my regex doesn't work.

Expand|Select|Wrap|Line Numbers
  1. import re
  2. p = re.compile(r'[A-Za-z]:\\([^/:\*?"<>\|])*')
  4. 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.

Jul 28 '06 #5
sorry i forgot to escape the question mark...
import re
p = re.compile(r'[A-Za-z]:\\([^/:\*?"<>\|])*')
even when I escape that it still doesnt work as expected.

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

p.match('c:\tes t') still returns None.

Jul 28 '06 #6
p = re.compile(r'[A-Za-z]:\\([^/:\*?"<>\|])*')
x = p.match("c:\tes t")
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:
>>s = "c:\test"
print s
c: est

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

s = r"c:\test"


s = "c:\\test"

which you'll find should now be successfully found with



Jul 28 '06 #7
Sybren Stuvel wrote:
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.


Jul 28 '06 #8
not sure why this passes:

>>regex = r'[A-Za-z]:\\([^/:\*\?"<>\|])*'
p = re.compile(rege x)
p.match('c:\\ test')
<_sre.SRE_Mat ch object at 0x009D77E0>
>>p.match('c:\\ test?:/')
<_sre.SRE_Mat ch object at 0x009D7720>
the last example shouldnt give a match

Jul 28 '06 #9
>>>regex = r'[A-Za-z]:\\([^/:\*\?"<>\|])*'
>>>p = re.compile(rege x)
p.match('c:\ \test')
<_sre.SRE_Mat ch object at 0x009D77E0>
>>>p.match('c:\ \test?:/')
<_sre.SRE_Mat ch object at 0x009D7720>

the last example shouldnt give a match
Ah, but it should, because it *does* match.
>>m = p.match('c:\\te st?:/')
>># add a "$" at the end to anchor it
# to the end of the line
regex = r'[A-Za-z]:\\([^/:\*\?"<>\|])*$'
p = re.compile(rege x)
m = p.match('c:\\te st?:/')
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.


Jul 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e"; Regex re = new Regex( string.Format(@"(+)", escaped), RegexOptions.CultureInvariant ); string s = re.Replace( input, "" ); It doesn't seem to work, regular expression return without filter out any character
by: derek.google | last post by:
I hope a Boost question is not too off-topic here. It seems that upgrading to Boost 1.33 broke some old regex code that used to work. I have reduced the problem to this simple example: cout << boost::regex_replace(string("foo"), boost::regex(".*"), string("bar")) << endl; The above code prints "barbar" where I expect "bar". Can anyone shed some light on this? It used to work with 1.30 (though regex_replace
by: James Dean | last post by:
I wanted to use regular expressions but unfortunetely it is too slow.....Should they be so slow or am i doing something wrong. I am reading in bytes from a file then converting them to char then making a string out of each of the individual bytes. I check if its in the correct format...and take out the various paretres i need. It looked nice and neat so i am not happy that i may have to use another method.....any alternative solutions?.
by: | last post by:
Here is an interesting one. Running asp.net 2.0 beta 2. I have a regular expression used in a regex validator that works on the client side in Firefox but not in IE. Any ideas? IE always reports the field is invalid. The expression is: ^(?!\d)(?=.*\d)(?=.*)(?=.*)(?=.*).{8,25}$ If I enter "Test_Field1" Firefox considers it valid on client side, IE doesnt. Server side considers it valid too because when I submit the form in
by: taylorjonl | last post by:
I am having a problem matching some text. It is a very simple pattern but it doesn't seem to work. Here goes. <td*>.*?</td> That is the pattern, it should match any <td></td> pair. Here is my input data. <td valign="top">Buyer<a href="http://www.google.com">google</a><img src="www.google.com/s.gif" width="4" border="0">(<a
by: ad | last post by:
I am useing VS2005 to develop wep application. I use a RegularExpress both in RegularExpressionValidator and Regex class to validate a value. The RegularExpress is 20|\-9|\-1|?\d{1} When I enter 33 and validate with RegularExpressionValidator, it fail to pass. But when I validate with regex class : Regex.IsMatch(Sight0L, @"20|\-9|\-1|?\d{1}");
by: =?Utf-8?B?amFj?= | last post by:
Hi, I have problems with following code and don’t find the bug : // Set ArrayList aArray = new ArrayList(); regStr = new Regex(@"\?)*(\d+)\]"); if(text != null && regStr.IsMatch(text)) {
by: apoorva.groups | last post by:
Hi I am facing problem while using regexec function. Ex: String = "abc_def_hig" sub string = "def" regexc if I use regexec the it will find the sub string in string and it will return 0. I want to modify the sub string such that it matches only if the
by: mikko.n | last post by:
I have recently been experimenting with GNU C library regular expression functions and noticed a problem with pattern matching. It seems to recognize only the first match but ignoring the rest of them. An example: mikko.c: ----- #include <stdio.h> #include <regex.h>
by: =?Utf-8?B?TWFya19C?= | last post by:
The following is working for me but I want to include numbers in scientific notation. public double Evaluate( string expr ) { const string Num = @"(\-?\d+\.?\d*|\-?\.\d+)" Regex reMulDiv = new Regex(Num + @"\s*()\s*" + Num); other stuff:
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.