Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'explicit': 0.07; 'matches': 0.07; 'subject:Questions': 0.07; 'ascii': 0.09; 'happen?': 0.09; 'literal': 0.09; 'matched': 0.09; 'cc:addr :python-list': 0.11; 'jan': 0.12; 'language,': 0.12; 'assume': 0.14; "'0',": 0.16; "'c',": 0.16; '8-bit': 0.16; 'backslash': 0.16; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'skip:[ 60': 0.16; 'underscore.': 0.16; 'wrote:': 0.18; '(but': 0.19; 'cc:addr:python.org': 0.22; 'affects': 0.24; 'byte': 0.24; 'string,': 0.24; 'unicode': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'character': 0.29; 'characters': 0.30; 'message- id:@mail.gmail.com': 0.30; 'adams': 0.31; 'regular': 0.32; 'run': 0.32; 'quite': 0.32; 'cases': 0.33; 'used,': 0.33; 'there,': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'ahead': 0.38; 'skip:[ 10': 0.38; 'expect': 0.39; 'does': 0.39; 'though,': 0.39; 'either': 0.39; 'catch': 0.60; 'most': 0.60; 'numbers': 0.61; 'entire': 0.61; "you're": 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'different': 0.65; 'alphanumeric': 0.68; 'btw),': 0.84; 'different.': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=XbpQumqo9zzTmpN7Bm4F8ZdQrPExt67K6/oAy+V4NK0=; b=nyp75g+zkDH2skQg0UP0nBLi6nLd04YcJriVyrFvbzbJ0stlHO2lf7gNg9Gu0j2S55 0mJGhk4+KT/kA0zR4uEQUVaaagC113OwfswExsDpFNNZX+9MYJLl2ys26aVzhVGPAltW QHbSWSHLhk/1vgPtONtzOITHNszFURiLSfmnjvqYbPOTFyRl1QSjVnTyUdaLe9r1edK6 dMmZOW5b7WOgdQMgwwnMhf4GHc/t3SBAH2dAKpwobPlNyERJ7yzInIc/+04kyC52K9jg OjdENX0yrFL94VcmAQC274WTyD+IovAhRJlxPvKLY5ebTqIbplqUJQREy0p7329lfSA/ 9VfQ== MIME-Version: 1.0 X-Received: by 10.66.66.234 with SMTP id i10mr25750231pat.127.1390756081091; Sun, 26 Jan 2014 09:08:01 -0800 (PST) In-Reply-To: <3f568767-e13a-4c7d-a4fb-85caca2adf6e@googlegroups.com> References: <3f568767-e13a-4c7d-a4fb-85caca2adf6e@googlegroups.com> Date: Mon, 27 Jan 2014 04:08:01 +1100 Subject: Re: re Questions From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390756093 news.xs4all.nl 2864 [2001:888:2000:d::a6]:55621 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64780 On Mon, Jan 27, 2014 at 3:59 AM, Blake Adams wrote: > If I want to set up a match replicating the '\w' pattern I would assume that would be done with '[A-z0-9_]'. However, when I run the following: > > re.findall('[A-z0-9_]','^;z %C\@0~_') it matches ['^', 'z', 'C', '\\', '0', '_']. I would expect the match to be ['z', 'C', '0', '_']. > > Why does this happen? Because \w is not the same as [A-z0-9_]. Quoting from the docs: """ \w For Unicode (str) patterns:Matches Unicode word characters; this includes most characters that can be part of a word in any language, as well as numbers and the underscore. If the ASCII flag is used, only [a-zA-Z0-9_] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [a-zA-Z0-9_] may be a better choice).For 8-bit (bytes) patterns:Matches characters considered alphanumeric in the ASCII character set; this is equivalent to [a-zA-Z0-9_]. """ If you're working with a byte string, then you're close, but A-z is quite different from A-Za-z. The set [A-z] is equivalent to [ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz] (that's a literal backslash in there, btw), so it'll also catch several non-alphabetic characters. With a Unicode string, it's quite distinctly different. Either way, \w means "word characters", though, so just go ahead and use it whenever you want word characters :) ChrisA