473,621 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ 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\co des\regextest.p y", line 27, in
-toplevel-
ultimate =
re.split(',|。|、 |!|:|,|?|“|”|)| (|;',
ch[0])
File "c:\python23\li b\sre.py", line 156, in split
return _compile(patter n, 0).split(string ,
maxsplit)
File "c:\python23\li b\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 9498
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\co des\regextest.p y", line 27, in
-toplevel-
ultimate =
re.split(',|。|、 |!|:|,|?|“|”|)| (|;',
ch[0])
File "c:\python23\li b\sre.py", line 156, in split
return _compile(patter n, 0).split(string ,
maxsplit)
File "c:\python23\li b\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
1602
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 expression."} ============== XSLT:
2
5355
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: "You entered an expression that has an invalid reference to the
6
30264
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 here...} I'm fairly new to C. What is wrong with the above? PS: All I'm trying to do is to get the first word on a question, so
4
27356
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 individual character, but would like to do it all in one step if possible. Here's what I have now: title = title.Replace("?", ""); title = title.Replace(":", "");
2
11705
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" statement. e.g. if(expression) return false; Later, I found that this error was due to a mistake in my method declaration. I had put void as a return type in my method. When I changed this to bool, everything works.
6
5794
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 <process.h> #include <windows.h> #pragma comment(lib,"libcmt.lib") __int64 Counter=0; BOOL volatile stop_thread = FALSE;
3
1359
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 expression to reject invalid files faster. The valid files are wellformed and looks something like this: Once upon a time
7
1516
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 followinge error results on the last line: Error 32 Type of conditional expression cannot be determined because there is no implicit conversion between 'Test.IDerived1' and 'Test.IDerived2'
2
3214
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, line 1, column 1, in my deploy project.) I double click on the error and it shows this... vti_encoding:SR|utf8-nl vti_timelastmodified:TR|09 May 2008 01:16:09 -0000
1
8306
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7127
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, 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...
1
6101
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4065
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...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
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
1
1763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1460
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 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.