Regex substitution trouble

TP wingusr at gmail.com
Tue Oct 28 14:25:00 CET 2014


On Tue, Oct 28, 2014 at 4:02 AM, <massi_srb at msn.com> wrote:

> Hi everyone,
>
> I'm not really sure if this is the right place to ask about regular
> expressions, but since I'm usin python I thought I could give a try :-)
> Here is the problem, I'm trying to write a regex in order to substitute
> all the occurences in the form $"somechars" with another string. This is
> what I wrote:
>
> newstring = re.sub(ur"""(?u)(\$\"[\s\w]+\")""", subst, oldstring)
>
> This works pretty well, but it has a problem, I would need it also to
> handle the case in which the internal string contains the double quotes,
> but only if preceeded by a backslash, that is something like
> $"somechars_with\\"doublequotes".
> Can anyone help me to correct it?
>
> Thanks in advance!
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Carefully reading the Strings section of "Example Regexes to Match Common
Programming Language Constructs" [1] should (with a bit of effort), solve
your problem I think. Note the use of the negated character class for one
thing.

[1] http://www.regular-expressions.info/examplesprogrammer.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141028/7a2a9e1c/attachment.html>


More information about the Python-list mailing list