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


Groups > comp.lang.python > #34497 > unrolled thread

regex walktrough

Started byrh <richard_hubbe11@lavabit.com>
First post2012-12-08 09:48 -0800
Last post2012-12-09 00:56 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  regex walktrough rh <richard_hubbe11@lavabit.com> - 2012-12-08 09:48 -0800
    Re: regex walktrough Hans Mulder <hansmu@xs4all.nl> - 2012-12-08 20:33 +0100
      Re: regex walktrough rh <richard_hubbe11@lavabit.com> - 2012-12-08 14:57 -0800
        Re: regex walktrough Hans Mulder <hansmu@xs4all.nl> - 2012-12-09 00:34 +0100
          Re: regex walktrough MRAB <python@mrabarnett.plus.com> - 2012-12-09 00:56 +0000

#34497 — regex walktrough

Fromrh <richard_hubbe11@lavabit.com>
Date2012-12-08 09:48 -0800
Subjectregex walktrough
Message-ID<mailman.627.1354988907.29569.python-list@python.org>
 Look through some code I found this and wondered about what it does:
^(?P<salsipuedes>[0-9A-Za-z-_.//]+)$

Here's my walk through:

1) ^ match at start of string
2) ?P<salsipuedes> if a match is found it will be accessible in a variable
salsipuedes
3) [0-9A-Za-z-_.//] this is the one that looks wrong to me, see below
4) + one or more from the preceeding char class
5) () the grouping we want returned (see #2)
6) $ end of the string to match against but before any newline


more on #3
the z-_ part looks wrong and seems that the - should be at the start
of the char set otherwise we get another range z-_ or does the a-z
preceeding the z-_ negate the z-_ from becoming a range?  The "."
might be ok inside a char set. The two slashes look wrong but maybe
it has some special meaning in some case? I think only one slash is
needed.

I've looked at pydoc re, but it's cursory.

[toc] | [next] | [standalone]


#34500

FromHans Mulder <hansmu@xs4all.nl>
Date2012-12-08 20:33 +0100
Message-ID<50c39613$0$6972$e4fe514c@news2.news.xs4all.nl>
In reply to#34497
On 8/12/12 18:48:13, rh wrote:
>  Look through some code I found this and wondered about what it does:
> ^(?P<salsipuedes>[0-9A-Za-z-_.//]+)$
> 
> Here's my walk through:
> 
> 1) ^ match at start of string
> 2) ?P<salsipuedes> if a match is found it will be accessible in a
> variable salsipuedes

I wouldn't call it a variable.  If m is a match-object produced
by this regex, then m.group('salsipuedes') will return the part
that was captured.

I'm not sure, though, why you'd want to define a group that
effectively spans the whole regex.  If there's a match, then
m.group(0) will return the matching substring, and
m.group('salsipuedes') will return the substring that matched
the parenthesized part of the pattern and these two substrings
will be equal, since the only bits of the pattern outside the
parenthesis are zero-width assertions.

> 3) [0-9A-Za-z-_.//] this is the one that looks wrong to me, see below
> 4) + one or more from the preceeding char class
> 5) () the grouping we want returned (see #2)
> 6) $ end of the string to match against but before any newline
> 
> more on #3
> the z-_ part looks wrong and seems that the - should be at the start
> of the char set otherwise we get another range z-_ or does the a-z
> preceeding the z-_ negate the z-_ from becoming a range?

The latter: a-z is a range and block the z-_ from being a range.
Consequently, the -_ bit matches only - and _.

> The "." might be ok inside a char set.

It is.  Most special characters lose their special meaning
inside a char set.

> The two slashes look wrong but maybe it has some special meaning
> in some case? I think only one slash is needed.

You're correct: there's no special meaning and only one slash
is needed.  But then, a char set is a set and duplcates are
simply ignored, so it does no harm.

Perhaps the person who wrote this was confusing slashes and
backslashes.

> I've looked at pydoc re, but it's cursory.

That's one way of putting it.


Hope this helps,

-- HansM

[toc] | [prev] | [next] | [standalone]


#34508

Fromrh <richard_hubbe11@lavabit.com>
Date2012-12-08 14:57 -0800
Message-ID<mailman.636.1355007486.29569.python-list@python.org>
In reply to#34500
On Sat, 08 Dec 2012 20:33:37 +0100
Hans Mulder <hansmu@xs4all.nl> wrote:

> On 8/12/12 18:48:13, rh wrote:
> >  Look through some code I found this and wondered about what it
> > does: ^(?P<salsipuedes>[0-9A-Za-z-_.//]+)$
> > 
> > Here's my walk through:
> > 
> > 1) ^ match at start of string
> > 2) ?P<salsipuedes> if a match is found it will be accessible in a
> > variable salsipuedes
> 
> I wouldn't call it a variable.  If m is a match-object produced
> by this regex, then m.group('salsipuedes') will return the part
> that was captured.
> 
> I'm not sure, though, why you'd want to define a group that
> effectively spans the whole regex.  If there's a match, then
> m.group(0) will return the matching substring, and
> m.group('salsipuedes') will return the substring that matched
> the parenthesized part of the pattern and these two substrings
> will be equal, since the only bits of the pattern outside the
> parenthesis are zero-width assertions.

Good point, it's making the re engine do extra work.
It's not my code and that's another gap in the author's proficiency.
(I don't know who the author is....FWIW)

> 
> > 3) [0-9A-Za-z-_.//] this is the one that looks wrong to me, see
> > below
> > 4) + one or more from the preceeding char class
> > 5) () the grouping we want returned (see #2)
> > 6) $ end of the string to match against but before any newline
> > 
> > more on #3
> > the z-_ part looks wrong and seems that the - should be at the start
> > of the char set otherwise we get another range z-_ or does the a-z
> > preceeding the z-_ negate the z-_ from becoming a range?
> 
> The latter: a-z is a range and block the z-_ from being a range.
> Consequently, the -_ bit matches only - and _.
> 
> > The "." might be ok inside a char set.
> 
> It is.  Most special characters lose their special meaning
> inside a char set.
> 
> > The two slashes look wrong but maybe it has some special meaning
> > in some case? I think only one slash is needed.
> 
> You're correct: there's no special meaning and only one slash
> is needed.  But then, a char set is a set and duplcates are
> simply ignored, so it does no harm.

I wonder if there's harm in the performance. Probably not
but regex is some tricky code and can be expensive even when written
well.  For example does this perform better than the original:
^(?P<salsipuedes>[-\w./]+)$

Not sure if the \w sequence includes the - or the . or the /
I think it does not.

> 
> Perhaps the person who wrote this was confusing slashes and
> backslashes.

Possibly.

> 
> > I've looked at pydoc re, but it's cursory.
> 
> That's one way of putting it.
> 
> 
> Hope this helps,

Does help, thanks.

> 
> -- HansM
> 
> 


-- 

[toc] | [prev] | [next] | [standalone]


#34510

FromHans Mulder <hansmu@xs4all.nl>
Date2012-12-09 00:34 +0100
Message-ID<50c3ce9d$0$6960$e4fe514c@news2.news.xs4all.nl>
In reply to#34508
On 8/12/12 23:57:48, rh wrote:
> Not sure if the \w sequence includes the - or the . or the /
> I think it does not.

You guessed right:

>>> [ c for c in 'x-./y' if re.match(r'\w', c) ]
['x', 'y']
>>>

So x and y match \w and  -, . and / do not.


Hope this helps,

-- HansM

[toc] | [prev] | [next] | [standalone]


#34513

FromMRAB <python@mrabarnett.plus.com>
Date2012-12-09 00:56 +0000
Message-ID<mailman.639.1355014609.29569.python-list@python.org>
In reply to#34510
On 2012-12-08 23:34, Hans Mulder wrote:
> On 8/12/12 23:57:48, rh wrote:
>> Not sure if the \w sequence includes the - or the . or the /
>> I think it does not.
>
> You guessed right:
>
>>>> [ c for c in 'x-./y' if re.match(r'\w', c) ]
> ['x', 'y']
>>>>
>
> So x and y match \w and  -, . and / do not.
>
This is shorter:

 >>> re.findall(r'\w', 'x-./y')
['x', 'y']

But remember that r"\w" is more than just r"[A-Za-z0-9_]" (unless
you're using ASCII).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web