Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Vlastimil Brom 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: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de rM87aml8/REjlhpElJxJAAE+yAALbiXiCuQJ6621gB3A== Return-Path: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109960 2016-06-15 5:28 GMT+02:00 Yubin Ruan : > Hi everyone, > I am struggling writing a right regex that match what I want: > > Problem Description: > > Given a string like this: > > >>>string = "false_head aaa bbb false_tail \ > true_head some_text_here ccc ddd eee true_tail" > > I want to match the all the text surrounded by those " ", > but only if those " " 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.*)([^<]+)(?=.*true_tail)", "false_head aaa bbb false_tail true_head some_text_here ccc ddd eee true_tail fff 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