Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74050
| Date | 2014-07-06 20:19 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: How to write this repeat matching? |
| References | <93a40570-00ed-4507-aa16-221d7e500468@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11555.1404674388.18130.python-list@python.org> (permalink) |
On 2014-07-06 19:57, rxjwg98@gmail.com wrote:
> Hi,
> On Python website, it says that the following match can reach 'abcb' in 6 steps:
>
> .............
> A step-by-step example will make this more obvious. Let's consider the expression
> a[bcd]*b. This matches the letter 'a', zero or more letters from the class [bcd],
> and finally ends with a 'b'. Now imagine matching this RE against the string
> abcbd.
>
> The end of the RE has now been reached, and it has matched abcb. This
> demonstrates how the matching engine goes as far as it can at first, and if no
> match is found it will then progressively back up and retry the rest of the RE
> again and again. It will back up until it has tried zero matches for [bcd]*, and
> if that subsequently fails, the engine will conclude that the string doesn't
> match the RE at all.
> .............
>
> I write the following code:
>
> .......
> import re
>
> line = "abcdb"
>
> matchObj = re.match( 'a[bcd]*b', line)
>
> if matchObj:
> print "matchObj.group() : ", matchObj.group()
> print "matchObj.group(0) : ", matchObj.group()
> print "matchObj.group(1) : ", matchObj.group(1)
> print "matchObj.group(2) : ", matchObj.group(2)
> else:
> print "No match!!"
> .........
>
> In which I have used its match pattern, but the result is not 'abcb'
>
That's because the example has 'abcb', but you have:
line = "abcdb"
(You've put a 'd' in it.)
> Only matchObj.group(0): abcdb
>
> displays. All other group(s) have no content.
>
There are no capture groups in your regex, only group 0 (the entire
matched part).
> How to write this greedy search?
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to write this repeat matching? rxjwg98@gmail.com - 2014-07-06 11:57 -0700
Re: How to write this repeat matching? MRAB <python@mrabarnett.plus.com> - 2014-07-06 20:19 +0100
Re: How to write this repeat matching? Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-06 13:26 -0600
Re: How to write this repeat matching? rxjwg98@gmail.com - 2014-07-07 06:30 -0700
Re: How to write this repeat matching? Anssi Saari <as@sci.fi> - 2014-07-07 18:48 +0300
Re: How to write this repeat matching? Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-07 10:18 -0600
csiph-web