Regular Expressions

R

Ron

This is probably a repeated question, but try as I might I was unable
to find something similar in the archives.

I'm trying to develop a regular expression for recognizing a simplified
C-Style string syntax. I need it to be able to handle escape sequences
of the form \x where x is any character including ".

Here's what I'm trying:

\"([^"\\]|(\\.))*\"

When I try to get it to recognize something like:

"I said, \"Hello!\""

It stops at the first quote after the \.

I've used this very same regular expression in a parser generator I
have for C++ and it works just fine.

Any thoughts on what I'm doing wrong in the Python Reg Ex world?

Thanks for the comments & help.

Ron
 
R

Roel Schroeven

Ron said:
This is probably a repeated question, but try as I might I was unable
to find something similar in the archives.

I'm trying to develop a regular expression for recognizing a simplified
C-Style string syntax. I need it to be able to handle escape sequences
of the form \x where x is any character including ".

Here's what I'm trying:

\"([^"\\]|(\\.))*\"

When I try to get it to recognize something like:

"I said, \"Hello!\""

It stops at the first quote after the \.

Works for me:
>>> print re.search(r'\"([^"\\]|(\\.))*\"',
.... r'"I said \"Hello!\""').group(0)
"I said \"Hello!\""


You can leave out the backslashes in fron of the first and last quotes
in the regex, by the way, at least if you use ' instead of " to delimite it:
print re.search(r'"([^"\\]|(\\.))*"',
.... r'"I said \"Hello!\""').group(0)
"I said \"Hello!\""
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top