Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109960
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: python regex: variable length of positive lookbehind assertion |
| Date | 2016-06-15 10:31 +0200 |
| Message-ID | <mailman.68.1465979485.2288.python-list@python.org> (permalink) |
| References | <c848333b-026d-4b40-9dfb-9c35d1f2b8f2@googlegroups.com> <CAHzaPENzs9Z0qqs3P8nZVcZK8rD==1VMo0ffHEdhTQrunStSLQ@mail.gmail.com> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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