Regular Expression bug?

jose isaias cabrera jicman at gmail.com
Thu Mar 2 14:38:27 EST 2023


On Thu, Mar 2, 2023 at 2:32 PM <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>
> On 2023-03-02 at 14:22:41 -0500,
> jose isaias cabrera <jicman at gmail.com> wrote:
>
> > For the RegExp Gurus, consider the following python3 code:
> > <code>
> > import re
> > s = "pn=align upgrade sd=2023-02-"
> > ro = re.compile(r"pn=(.+) ")
> > r0=ro.match(s)
> > >>> print(r0.group(1))
> > align upgrade
> > </code>
> >
> > This is wrong. It should be 'align' because the group only goes up-to
> > the space. Thoughts? Thanks.
>
> The bug is in your regular expression; the plus modifier is greedy.
>
> If you want to match up to the first space, then you'll need something
> like [^ ] (i.e., everything that isn't a space) instead of that dot.

Thanks. I appreciate your wisdom.

josé
-- 

What if eternity is real?  Where will you spend it?  Hmmmm...


More information about the Python-list mailing list