Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; 'newline': 0.07; 'python': 0.09; '(it': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'looked': 0.10; 'dec': 0.15; 'sat,': 0.15; '"-"': 0.16; '"."': 0.16; '"/"': 0.16; '"]"': 0.16; 'literals.': 0.16; 'range,': 0.16; 're,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 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; '+0000': 0.20; 'followed': 0.20; 'thanks.': 0.21; 'needed.': 0.23; "i've": 0.23; 'seems': 0.23; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:[ 10': 0.26; '(see': 0.27; 'wonder': 0.27; 'set.': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'grouping': 0.29; 'reduced': 0.29; 'character': 0.29; 'class': 0.29; 'maybe': 0.29; 'returned': 0.30; 'code': 0.31; 'accessible': 0.33; 'defining': 0.33; 'to:addr :python-list': 0.33; 'another': 0.33; "can't": 0.34; 'wrong': 0.34; 'too.': 0.35; "won't": 0.35; 'received:org': 0.36; 'but': 0.36; "wasn't": 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'itself': 0.37; 'ok,': 0.37; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'there,': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'help': 0.40; 'end': 0.40; 'think': 0.40; 'range': 0.60; 'is.': 0.62; 'more': 0.63; 'unnecessary': 0.65; 'walk': 0.71; 'special': 0.73; 'you:': 0.75; 'case?': 0.84; 'object:': 0.84; 'received:sd.cox.net': 0.84; 'doubling': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: rh Subject: Re: regex walktrough Date: Sat, 8 Dec 2012 14:19:40 -0800 References: <20121208094813.5560b42fb233329a44b05dee@lavabit.com> <50C38224.3040201@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ip184-187-163-27.sb.sd.cox.net User-Agent: dsodnetnin X-Mailer: EZnn0.37p X-Newsreader: EZnn0.37p X-Gmane-NNTP-Posting-Host: EZnn0.37p Original-Received: from slem by 1.1 with local X-No-Archive: yes Archive: no X-Archive: expiry=11 X-Archive: encrypt X-Operating-System: Barebones_6.1 X-Gmane-NNTP-Posting-Host: 192.168.1.1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355005200 news.xs4all.nl 6840 [2001:888:2000:d::a6]:36228 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34504 On Sat, 08 Dec 2012 18:08:36 +0000 MRAB wrote: > On 2012-12-08 17:48, rh wrote: > > Look through some code I found this and wondered about what it > > does: ^(?P[0-9A-Za-z-_.//]+)$ > > > > Here's my walk through: > > > > 1) ^ match at start of string > > 2) ?P 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[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\-]"). Handy tip there, thanks. re.compile(r"^(?P[-\w./]+)$", flags=re.DEBUG) at at_beginning subpattern 1 max_repeat 1 65535 in literal 45 category category_word literal 46 literal 47 at at_end I reduced the expression too. Now I wonder why re.DEBUG doesn't unroll category_word. Some other re flag? > > As for "(?P...)", 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[a-z]+)", "xyz") > >>> m.group("foo") > 'xyz' > Ok, "named group" it is.