473,487 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

v # invalid expression ?

I am trying to split a string like so:

re.split(',|。|、|!|:|,|?|“|”|)|(|;',
my_string)

Python complains like so:

Traceback (most recent call last):
File "C:\Python23\codes\regextest.py", line 27, in
-toplevel-
ultimate =
re.split(',|。|、|!|:|,|?|“|”|)|(|;',
ch[0])
File "c:\python23\lib\sre.py", line 156, in split
return _compile(pattern, 0).split(string,
maxsplit)
File "c:\python23\lib\sre.py", line 229, in _compile
raise error, v # invalid expression
error: nothing to repeat

Does this mean that the regular expression I pass to
the split function is not valid?

That expression is just like ',|)|#|!', that is, an
or-ed list of punctuations. Then why invalid?

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com

Jul 18 '05 #1
1 9484
Anthony Liu wrote:
I am trying to split a string like so:

re.split(',|。|、|!|:|,|?|“|”|)|(|;',
my_string)

Python complains like so:

Traceback (most recent call last):
File "C:\Python23\codes\regextest.py", line 27, in
-toplevel-
ultimate =
re.split(',|。|、|!|:|,|?|“|”|)|(|;',
ch[0])
File "c:\python23\lib\sre.py", line 156, in split
return _compile(pattern, 0).split(string,
maxsplit)
File "c:\python23\lib\sre.py", line 229, in _compile
raise error, v # invalid expression
error: nothing to repeat

Does this mean that the regular expression I pass to
the split function is not valid?

That expression is just like ',|)|#|!', that is, an
or-ed list of punctuations. Then why invalid?


Because ?, (, and ) are special characters in regular expressions. You must
escape them with a backslash:
re.split(',|。|、|!|:|,|\\?|\\)|\\(|;', 'foo,bar(spam)')

['foo', 'bar', 'spam', '']

HTH
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2

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

Similar topics

1
1596
by: Steve | last post by:
Hello, I get an error in my xslt document using vs.net 2005 and Xml Spy. Any help is greatly appreciated! ================ Error: ================ {"'$Field/name()' is an invalid XPath...
2
5343
by: Arnold | last post by:
Hey there, I just put the following code: DoCmd.GoToRecord , , acLast in the On Open event of my form, which has 2 subforms. The following error message appears when the form now opens: ...
6
30212
by: Marty M | last post by:
The following causes the "invalid initializer" message during gcc compile time... char firstword = word(question,1); the "word" function is... char * word(char *phrase, int what) {...body...
4
27338
by: GregMa | last post by:
Does anyone have a good regex expression to replace any invalid filename characters in a string? Those characters are: /, \, :, *, ?, ", <, >, | I have it right now with string.replace for each...
2
11697
by: Raj | last post by:
Hi, I was getting an error in my program saying that return keyword must not be followed by an object expression. I was confused first because I was using return statement in a valid "if"...
6
5771
by: | last post by:
The following code snippet can be build in VC 6.0, but failed in VC 2003. //////////////save the following code in t.cpp #define _MT #define _WIN32_WINNT 0x0500 #include <iostream> #include...
3
1347
by: Bertrand | last post by:
Hi, I am using a regular expression to read records from a text file. But when reading files with invalid formats it takes ages before the program rjects the file. So I want to optimise the...
7
1509
by: Larry Smith | last post by:
IDerived1 derived1 = GetDerived1(); IDerived2 derived2 = GetDerived2(); IBase ibase = (derived1 != null ? derived1 : derived2); Where "IDerived1" and "IDerived2" each inherit from "IBase". The...
2
3207
by: Cirene | last post by:
My ASP.NET project builds fine (VS2008). I added a new web deployment project. When I try to build it I get: Data at the root level is invalid. Line1, position 1. (The file is web.config,...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
Oralloy
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,...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
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,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
266
bsmnconsultancy
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...

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.