Regular Expressions
Roel Schroeven
rschroev_nospam_ml at fastmail.fm
Wed Mar 23 05:51:41 EST 2005
Ron wrote:
> 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!\""
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton
Roel Schroeven
More information about the Python-list
mailing list