Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'enabling': 0.05; 'skip:` 10': 0.05; 'transform': 0.05; 'incompatible': 0.07; 'matches': 0.07; 'python': 0.09; 'backwards': 0.09; 'subject:ignore': 0.09; 'unicode,': 0.09; 'library': 0.15; 'dec': 0.15; '3.3,': 0.16; 'ascii,': 0.16; 'german.': 0.16; 'insensitive': 0.16; 'lowercase': 0.16; 'match:': 0.16; 'matching.': 0.16; 'roy': 0.16; 'sharp': 0.16; 'subject:case': 0.16; 'uppercase': 0.16; 'wrote:': 0.17; '(in': 0.18; '>>>': 0.18; 'equivalent': 0.20; 'addition,': 0.21; 'not,': 0.21; 'posted': 0.22; 'matching': 0.23; 'originally': 0.23; 'header:In-Reply-To:1': 0.25; 'supported': 0.26; 'first,': 0.27; 'rules': 0.27; 'converting': 0.27; 'message- id:@mail.gmail.com': 0.27; 'correct': 0.28; "d'aprano": 0.29; 'enforce': 0.29; 'header,': 0.29; 'steven': 0.29; 'url:mailman': 0.29; '"the': 0.29; 'probably': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'mode': 0.30; 'url:python': 0.32; 'german': 0.32; "skip:' 20": 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'affects': 0.33; 'curious': 0.33; 'to:addr:python-list': 0.33; 'typically': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'doing': 0.35; 'expected': 0.35; 'received:209.85.220': 0.35; 'sometimes': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'skip:u 20': 0.36; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'correctly': 0.37; 'skip:v 20': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'nothing': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'build': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'lower': 0.61; 'mentioned': 0.63; 'more': 0.63; 'show': 0.63; 'wanting': 0.65; 'capital': 0.68; '8bit%:10': 0.69; 'smith': 0.71; 'unusual': 0.71; 'flag.': 0.84; 'fonts': 0.84; 'sensitivity': 0.84 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:to :content-type:content-transfer-encoding; bh=yzHIJISfLiKRNX4n5sdwabIOjIkaRynQ/ZmrP4SfnQU=; b=cMn/hPE2S3zxrEBsDPMylYQR0dOkKT62ZNNI69wmJvymzAMK8zT/DLaMxwyfVv0KmQ d6eJjSxaSk38/Ikwcdce498qPne8AvwCCgifaWVjA7tV9aWwhZ5qyvBjNpPUfQuILWxG VDG5vALX2b5IAxbWAsVZ7HnkEpiA3a7gR/j7jffZAPMLXYgIvU3/Rsjr4Qw6qFiaRSPY J6eU23vYlPKZc8O0sSx/aoVHIkmKEJTTd9IyC0TSYir/7/Qgl8+IyHkBdpvRZEwY9gme UshulH3SVErM44NLA64Q92Vs5VPlbkBp3oyai+zSw+xopH1iUydcRTF7yvqFe/0n+dbb BZFA== MIME-Version: 1.0 In-Reply-To: <50e262a1$0$30003$c3e8da3$5496439d@news.astraweb.com> References: <50e02485$0$3109$ba620e4c@news.skynet.be> <50e262a1$0$30003$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 2 Jan 2013 00:09:45 +0100 Subject: Re: ignore case only for a part of the regex? From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 111 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357081794 news.xs4all.nl 6911 [2001:888:2000:d::a6]:60593 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35928 2013/1/1 Steven D'Aprano : > On Sun, 30 Dec 2012 10:20:19 -0500, Roy Smith wrote: > >> The way I would typically do something like this is build my regexes in >> all lower case and .lower() the text I was matching against them. I'm >> curious what you're doing where you want to enforce case sensitivity in >> one part of a header, but not in another. > > Well, sometimes you have things that are case sensitive, and other things > which are not, and sometimes you need to match them at the same time. I > don't think this is any more unusual than (say) wanting to match an > otherwise lowercase word whether or not it comes at the start of a > sentence: > > "[Pp]rogramming" > > is conceptually equivalent to "match case-insensitive `p`, and case- > sensitive `rogramming`". > > > By the way, although there is probably nothing you can (easily) do about > this prior to Python 3.3, converting to lowercase is not the right way to > do case-insensitive matching. It happens to work correctly for ASCII, but > it is not correct for all alphabetic characters. > > > py> 'Stra=C3=9Fe'.lower() > 'stra=C3=9Fe' > py> 'Stra=C3=9Fe'.upper() > 'STRASSE' > > > The right way is to casefold first, then match: > > py> 'Stra=C3=9Fe'.casefold() > 'strasse' > > > Curiously, there is an uppercase =C3=9F in old German. In recent years so= me > typographers have started using it instead of SS, but it's still rare, > and the official German rules have =C3=9F transform into SS and vice vers= a. > It's in Unicode, but few fonts show it: > > py> unicodedata.lookup('LATIN CAPITAL LETTER SHARP S') > '=E1=BA=9E' > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list Hi, just for completeness, the mentioned regex library can take care of casfolding in case insensitive matching (in all supported versions: Python 2.5-2.7 and 3.1-3.3); i.e.: # case sensitive match: >>> for m in regex.findall(ur"Stra=C3=9Fe", u" STRA=C3=9FE STRASSE STRA=E1= =BA=9EE Strasse Stra=C3=9Fe "): print m ... Stra=C3=9Fe # case insensitive match: >>> for m in regex.findall(ur"(?i)Stra=C3=9Fe", u" STRA=C3=9FE STRASSE STRA= =E1=BA=9EE Strasse Stra=C3=9Fe "): print m ... STRA=C3=9FE STRA=E1=BA=9EE Stra=C3=9Fe # case insensitive match with casefolding: >>> for m in regex.findall(ur"(?if)Stra=C3=9Fe", u" STRA=C3=9FE STRASSE STR= A=E1=BA=9EE Strasse Stra=C3=9Fe "): print m ... STRA=C3=9FE STRASSE STRA=E1=BA=9EE Strasse Stra=C3=9Fe >>> >>> # after enabling the backwards incompatible modern matching behaviour, casefolding is by default turned on for case insensitive matches >>> for m in regex.findall(ur"(?V1i)Stra=C3=9Fe", u" STRA=C3=9FE STRASSE ST= RA=E1=BA=9EE Strasse Stra=C3=9Fe "): print m ... STRA=C3=9FE STRASSE STRA=E1=BA=9EE Strasse Stra=C3=9Fe >>> As a small addition, the originally posted pattern r'^Msg-(?:(?i)id):' would actually work as expected in this modern matching mode in regex - enabled with the V1 flag. In this case the flag-setting (?i) only affects the following parts of the pattern, not the whole pattern like in the current "re" and V0-compatibility-mode "regex" >>> regex.findall(r"(?V1)Msg-(?:(?i)id):", "the regex should match Msg-id:= , Msg-Id:, ... but not msg-id:, MSG-ID: and so on") ['Msg-id:', 'Msg-Id:'] >>> regards, vbr