Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90896

Re: Help with Regular Expression

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'insert': 0.05; 'string.': 0.05; 'correct.': 0.07; 'test,': 0.07; 'string': 0.09; 'skip:$ 20': 0.09; 'cc:addr:python-list': 0.11; 'subject:Help': 0.11; 'python': 0.11; 'wrote': 0.14; '-tkc': 0.16; 'advance!': 0.16; 'disallow': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'inputs': 0.16; 'occurences': 0.16; 'record,': 0.16; 'string)': 0.16; 'subject:Regular': 0.16; 'substitute': 0.16; 'unneeded': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'precise': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'exclude': 0.31; 'anyone': 0.31; 'another': 0.32; 'subject:with': 0.35; 'case,': 0.35; 'but': 0.35; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'should': 0.36; 'received:10': 0.37; 'clear': 0.37; 'expected': 0.38; 'little': 0.38; 'expression': 0.60; 'more': 0.64; 'here': 0.66; 'skip:$ 10': 0.81; 'skip:] 10': 0.84; 'to:addr:msn.com': 0.84
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1432052982212:2799707421
X-MC-Ingress-Time 1432052982212
Date Tue, 19 May 2015 11:30:14 -0500
From Tim Chase <python.list@tim.thechases.com>
To massi_srb@msn.com
Cc python-list@python.org
Subject Re: Help with Regular Expression
In-Reply-To <960db3a7-54e8-40dc-83f3-4e7e8f675529@googlegroups.com>
References <960db3a7-54e8-40dc-83f3-4e7e8f675529@googlegroups.com>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AuthUser tim@thechases.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.154.1432069261.17265.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432069261 news.xs4all.nl 2903 [2001:888:2000:d::a6]:43012
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90896

Show key headers only | View raw


On 2015-05-19 06:42, massi_srb@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






Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Help with Regular Expression massi_srb@msn.com - 2015-05-19 06:42 -0700
  Re: Help with Regular Expression Tim Chase <python.list@tim.thechases.com> - 2015-05-19 11:30 -0500

csiph-web