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


Groups > comp.lang.python > #109960

Re: python regex: variable length of positive lookbehind assertion

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Vlastimil Brom <vlastimil.brom@gmail.com>
Newsgroups comp.lang.python
Subject Re: python regex: variable length of positive lookbehind assertion
Date Wed, 15 Jun 2016 10:31:22 +0200
Lines 51
Message-ID <mailman.68.1465979485.2288.python-list@python.org> (permalink)
References <c848333b-026d-4b40-9dfb-9c35d1f2b8f2@googlegroups.com> <CAHzaPENzs9Z0qqs3P8nZVcZK8rD==1VMo0ffHEdhTQrunStSLQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de rM87aml8/REjlhpElJxJAAE+yAALbiXiCuQJ6621gB3A==
Return-Path <vlastimil.brom@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'that?': 0.05; '(especially': 0.07; 'repeated': 0.07; 'patterns,': 0.09; 'surrounded': 0.09; 'python': 0.10; 'subject:python': 0.14; 'everyone,': 0.15; '(besides': 0.16; 'expressions,': 0.16; 'fuzzy': 0.16; 'intended,': 0.16; 'massively': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'regex,': 0.16; 'subject:variable': 0.16; 'string': 0.17; 'variable': 0.18; '>>>': 0.20; 'library': 0.20; 'parsing': 0.22; 'matching': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'operations,': 0.27; 'regular': 0.29; 'character': 0.29; 'comments': 0.30; 'url:mailman': 0.30; 'certainly': 0.30; 'skip:[ 10': 0.31; "i'd": 0.31; 'generally': 0.32; 'problem': 0.33; 'url:python': 0.33; 'enhanced': 0.33; 'url:listinfo': 0.34; 'previous': 0.34; 'received:google.com': 0.35; 'behind': 0.35; 'text': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'thanks': 0.37; 'received:209': 0.38; 'hi,': 0.38; 'data': 0.39; 'does': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'suitable': 0.61; 'skip:u 10': 0.61; 'aaa': 0.84; 'suits': 0.84; 'to:name:python': 0.84; 'want:': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=v5AKB78OTp7rAUi7edKNwekJixDa4JPC7U2a9sAncMw=; b=CWujZs9mPpvULKh7POM0flMdxVx6jQcVLqyg4YUkhutRPKbzyNC+SSayfpgaFj19xT tZ19nyUqAXqXEKDnK8pf96ZGGtZ6BMjElpeegVL89YXZK+aQctOHOmEnOjhlISHv29st Oa9Zo+k9PEFw0ufXMrI+vq9Sad6VWSO7Na2deGp5aCllIeRdfbZCmXCgLy2QSkN0gnus aYAwXqqJMjvaKDvvG6MyuCsRjmTv1HgeQ15R0XcUFz/gtEbDehkDeVeWLv7qz9Jkpj25 8o9lx1yuauyNvDkwAGg1QQEWMMc5GS/2yFNssbHbYWiSyQNFx286ubzPcD3V7H/2BTVU mFBQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=v5AKB78OTp7rAUi7edKNwekJixDa4JPC7U2a9sAncMw=; b=aEpjCmrx+qKu6IuMbGWUD1rukZpk+V+vECJl7G3/QGE9RmoXAucZn1VwLlyKME6Uaa mWPs40vxzz3OuO22nx1JrR0MoPDdRnrMJZkSD+njBIoQlPECzBfZbKIbuxlXatr1oYyx Tn2M552yQc3ko4t1yrQw+81YL7R9DER2JHQTtM+gCeatC4+z7/ylGmmwYY7105s6+WtS /vCIcL3BcrMxiTVQOT809RxksF8wDskyuB3gaWdAsYNE+kuL0JzOrshvzTESW54+qsrd YuPxHYqmKHoDwGwF/Yge5Jjl0OfrmX4EEKWw7L15fgnPYacpd7LNpxKJGpvwOftAN4mO g8Rw==
X-Gm-Message-State ALyK8tK2LRvq9UMZTBfM2hARuCJQ9hSJr42dhOxU+F5MCsn1uvYkSOwd7AmBhV5YYUGEhea+8sQGPSSOf01v8A==
X-Received by 10.46.9.198 with SMTP id 189mr4861222ljj.5.1465979483034; Wed, 15 Jun 2016 01:31:23 -0700 (PDT)
In-Reply-To <c848333b-026d-4b40-9dfb-9c35d1f2b8f2@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CAHzaPENzs9Z0qqs3P8nZVcZK8rD==1VMo0ffHEdhTQrunStSLQ@mail.gmail.com>
X-Mailman-Original-References <c848333b-026d-4b40-9dfb-9c35d1f2b8f2@googlegroups.com>
Xref csiph.com comp.lang.python:109960

Show key headers only | View raw


2016-06-15 5:28 GMT+02:00 Yubin Ruan <ablacktshirt@gmail.com>:
> Hi everyone,
> I am struggling writing a right regex that match what I want:
>
> Problem Description:
>
> Given a string like this:
>
>     >>>string = "false_head <a>aaa</a> <a>bbb</a> false_tail \
>              true_head some_text_here <a>ccc</a> <a>ddd</a> <a>eee</a> true_tail"
>
> I want to match the all the text surrounded by those "<a> </a>",
> but only if those "<a> </a>" locate **in some distance** behind "true_head". That is, I expect to result to be like this:
>
>     >>>import re
>     >>>result = re.findall("the_regex",string)
>     >>>print result
>     ["ccc","ddd","eee"]
>
> How can I write a regex to match that?
> I have try to use the **positive lookbehind assertion** in python regex,
> but it does not allowed variable length of lookbehind.
>
> Thanks in advance,
> Ruan
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
html-like data is generally not very suitable for parsing with regex,
as was explained in the previous answers (especially if comments and
nesting are massively involved).
However, if this suits your data and the usecase, you can use regex
with variable-length lookarounds in a much enhanced "regex" library
for python
https://pypi.python.org/pypi/regex

your pattern might then simply have the form you most likely have
intended, e.g.:
>>> regex.findall(r"(?<=true_head.*)<a>([^<]+)</a>(?=.*true_tail)", "false_head <a>aaa</a> <a>bbb</a> false_tail true_head some_text_here <a>ccc</a> <a>ddd</a> <a>eee</a> true_tail <a>fff</a> another_false_tail")
['ccc', 'ddd', 'eee']
>>>

If you are accustomed to use regular expressions, I'd certainly
recommend this excellent library (besides unlimited lookarounds, there
are repeated and recursive patterns, many unicode-related
enhancements, powerful character set operations, even fuzzy matching
and much more).

hth,
   vbr

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


Thread

python regex: variable length of positive lookbehind assertion Yubin Ruan <ablacktshirt@gmail.com> - 2016-06-14 20:28 -0700
  Re: python regex: variable length of positive lookbehind assertion Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-14 21:18 -0700
    Re: python regex: variable length of positive lookbehind assertion Yubin Ruan <ablacktshirt@gmail.com> - 2016-06-14 21:38 -0700
  Re: python regex: variable length of positive lookbehind assertion Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-15 08:10 +0300
  Re: python regex: variable length of positive lookbehind assertion Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-06-15 10:31 +0200
  Re: python regex: variable length of positive lookbehind assertion alister <alister.ware@ntlworld.com> - 2016-06-15 12:27 +0000
    Re: python regex: variable length of positive lookbehind assertion Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-15 15:55 +0300
      Re: python regex: variable length of positive lookbehind assertion Marko Rauhamaa <marko@pacujo.net> - 2016-06-15 17:33 +0300
        Re: python regex: variable length of positive lookbehind assertion Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-15 17:57 +0300
          Re: python regex: variable length of positive lookbehind assertion Michael Torrie <torriem@gmail.com> - 2016-06-15 18:40 -0600
            Re: python regex: variable length of positive lookbehind assertion Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-16 08:39 +0300
              Re: python regex: variable length of positive lookbehind assertion Marko Rauhamaa <marko@pacujo.net> - 2016-06-16 09:03 +0300
      Re: python regex: variable length of positive lookbehind assertion alister <alister.ware@ntlworld.com> - 2016-06-15 15:31 +0000
        Re: python regex: variable length of positive lookbehind assertion Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-06-15 19:04 +0300

csiph-web