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


Groups > comp.lang.python > #34499

Re: regex walktrough

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'newline': 0.07; 'python': 0.09; '(it': 0.09; 'literal': 0.09; 'looked': 0.10; '"-"': 0.16; '"."': 0.16; '"/"': 0.16; '"]"': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'literals.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'range,': 0.16; 're,': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'set:': 0.16; 'skip:^ 20': 0.16; 'slash': 0.16; 'slashes': 0.16; 'unescaped': 0.16; 'string': 0.17; 'wrote:': 0.17; 'char': 0.17; '>>>': 0.18; 'variable': 0.20; 'followed': 0.20; 'needed.': 0.23; "i've": 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'looks': 0.26; 'skip:[ 10': 0.26; '(see': 0.27; 'set.': 0.27; 'grouping': 0.29; 'received:192.168.1.3': 0.29; 'character': 0.29; 'class': 0.29; 'maybe': 0.29; 'returned': 0.30; 'code': 0.31; 'received:84': 0.32; 'accessible': 0.33; 'defining': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; "can't": 0.34; 'wrong': 0.34; "won't": 0.35; 'but': 0.36; "wasn't": 0.36; 'should': 0.36; 'itself': 0.37; 'does': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'help': 0.40; 'end': 0.40; 'think': 0.40; 'range': 0.60; 'more': 0.63; 'unnecessary': 0.65; 'header:Reply-To:1': 0.68; 'walk': 0.71; 'reply-to:no real name:2**0': 0.72; 'special': 0.73; 'you:': 0.75; 'case?': 0.84; 'object:': 0.84; 'reply-to:addr:python.org': 0.84; 'doubling': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=O5K7TWBW c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=7Gjc_PwvzGwA:10 a=-L2ZZB-B5nRIUZPLvnsA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Sat, 08 Dec 2012 18:08:36 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0
MIME-Version 1.0
To python-list@python.org
Subject Re: regex walktrough
References <20121208094813.5560b42fb233329a44b05dee@lavabit.com>
In-Reply-To <20121208094813.5560b42fb233329a44b05dee@lavabit.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.629.1354990113.29569.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354990113 news.xs4all.nl 6949 [2001:888:2000:d::a6]:59692
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34499

Show key headers only | View raw


On 2012-12-08 17:48, 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
> 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.
>
Python itself will help you:

 >>> re.compile(r"^(?P<salsipuedes>[0-9A-Za-z-_.//]+)$", flags=re.DEBUG)
at at_beginning
subpattern 1
   max_repeat 1 65535
     in
       range (48, 57)
       range (65, 90)
       range (97, 122)
       literal 45
       literal 95
       literal 46
       literal 47
       literal 47
at at_end

Inside the character set: "0-9", "A-Z" and "a-z" are ranges; "-", "_",
"." and "/" are literals. Doubling the "/" is unnecessary (it has no
special meaning). "-" is a literal because it immediately follows a
range, so it can't be defining another range (if it immediately
followed a literal and wasn't immediately followed by an unescaped "]"
then it would, so r"[a-]" is the same as r"[a\-]").

As for "(?P<salsipuedes>...)", it won't be accessible in a variable
"salsipuedes", but will be accessible as a named group in the match
object:

 >>> m = re.match(r"(?P<foo>[a-z]+)", "xyz")
 >>> m.group("foo")
'xyz'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: regex walktrough MRAB <python@mrabarnett.plus.com> - 2012-12-08 18:08 +0000

csiph-web