Help with Regular Expression

Tim Chase python.list at tim.thechases.com
Tue May 19 12:30:14 EDT 2015


On 2015-05-19 06:42, massi_srb at msn.com wrote:
> I succesfully wrote a regex in python in order to substitute all
> the occurences in the form $"somechars" with another string. Here
> it is:
> 
> re.sub(ur"""(?u)(\$\"[^\"\\]*(?:\\.[^\"\\]*)*\")""", newstring,
> string)

The expression is a little more precise than you describe it, but the
general idea is correct.

For the record, the "(?u)" happens to be unneeded here, and I find it
more clear if you pass the re.UNICODE flag to the function.

> Now I would need to exclude from the match all the string in the
> form $", ", can anyone help me to modufy it? Thanks in advance!

If you don't want commas or spaces, you should be able to just insert
them into your various negated character-classes:

  r"""(?u)(\$\"[^\"\\, ]*(?:\\.[^\"\\, ]*)*\")"""

Unless you want to allow commas and/or spaces, but disallow commas
followed by spaces.  That's a lot uglier.  If that's the case, it
would help to have a test-harness of your expected inputs and results:

  re_to_test = r"""(?u)(\$\"[^\"\\, ]*(?:\\.[^\"\\, ]*)*\")"""
  for test, expected in [
     ('Hello $"who"!', 'Hello world!'),
     ('Hello $"who.who"!', 'Hello world!'),
     ('Hello $"who.is.it"!', 'Hello world!'),
     ('Hello $"who, what"!', 'Hello world!'),
     ('Hello $"who,what,where"!', 'Hello world!'),
     ('Hello $"who, what, where"!', 'Hello $"who, what, where"!'),
     ('Hello $"who is it"!', 'Hello world!'),
     ]:
    result = re.sub(re_to_test, "world", test, re.UNICODE)
    if result == expected:
      report_passing(...)
    else:
      report_failure(...)

-tkc










More information about the Python-list mailing list