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


Groups > comp.lang.python > #35987

Re: ignore case only for a part of the regex?

Path csiph.com!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <wxjmfauth@gmail.com>
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; 'table.': 0.07; 'python': 0.09; 'backwards': 0.09; 'janvier': 0.09; 'subject:ignore': 0.09; 'to:addr:comp.lang.python': 0.09; 'unicode,': 0.09; 'cc:addr :python-list': 0.10; '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; '\xc3\xa9crit\xc2\xa0:': 0.16; 'wrote:': 0.17; 'unicode': 0.17; '(in': 0.18; '>>>': 0.18; 'equivalent': 0.20; 'addition,': 0.21; 'not,': 0.21; 'posted': 0.22; 'cc:2**0': 0.23; 'matching': 0.23; 'originally': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'supported': 0.26; 'first,': 0.27; 'rules': 0.27; 'converting': 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; 'code': 0.31; '(and': 0.32; 'problem.': 0.32; 'url:python': 0.32; '-----': 0.32; 'german': 0.32; "skip:' 20": 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'affects': 0.33; 'curious': 0.33; 'point,': 0.33; 'typically': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'so,': 0.35; 'doing': 0.35; 'expected': 0.35; 'sometimes': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'das': 0.36; '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; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'nothing': 0.38; 'instead': 0.39; 'build': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'url:mail': 0.40; 'think': 0.40; 'from:no real name:2**0': 0.60; '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; '2013': 0.84; 'check)': 0.84; 'excellent.': 0.84; 'flag.': 0.84; 'fonts': 0.84; 'grosse': 0.84; 'sensitivity': 0.84; 'serious': 0.98
Newsgroups comp.lang.python
Date Wed, 2 Jan 2013 06:17:04 -0800 (PST)
In-Reply-To <mailman.1536.1357081794.29569.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=178.198.163.217; posting-account=ung4FAoAAAC46zhHJ0Nsnuox7M5gDvs_
References <50e02485$0$3109$ba620e4c@news.skynet.be> <roy-4F8381.10201930122012@news.panix.com> <50e262a1$0$30003$c3e8da3$5496439d@news.astraweb.com> <mailman.1536.1357081794.29569.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 178.198.163.217
MIME-Version 1.0
Subject Re: ignore case only for a part of the regex?
From wxjmfauth@gmail.com
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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>
Message-ID <mailman.1566.1357136227.29569.python-list@python.org> (permalink)
Lines 236
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357136227 news.xs4all.nl 6858 [2001:888:2000:d::a6]:37221
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35987

Show key headers only | View raw


Le mercredi 2 janvier 2013 00:09:45 UTC+1, Vlastimil Brom a écrit :
> 2013/1/1 Steven D'Aprano <steve+comp.lang.python@pearwood.info>:
> 
> > 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ße'.lower()
> 
> > 'straße'
> 
> > py> 'Straße'.upper()
> 
> > 'STRASSE'
> 
> >
> 
> >
> 
> > The right way is to casefold first, then match:
> 
> >
> 
> > py> 'Straße'.casefold()
> 
> > 'strasse'
> 
> >
> 
> >
> 
> > Curiously, there is an uppercase ß in old German. In recent years some
> 
> > typographers have started using it instead of SS, but it's still rare,
> 
> > and the official German rules have ß transform into SS and vice versa.
> 
> > It's in Unicode, but few fonts show it:
> 
> >
> 
> > py> unicodedata.lookup('LATIN CAPITAL LETTER SHARP S')
> 
> > 'ẞ'
> 
> >
> 
> >
> 
> >
> 
> > --
> 
> > 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ße", u" STRAßE STRASSE STRAẞE Strasse Straße "): print m
> 
> ...
> 
> Straße
> 
> 
> 
> # case insensitive match:
> 
> >>> for m in regex.findall(ur"(?i)Straße", u" STRAßE STRASSE STRAẞE Strasse Straße "): print m
> 
> ...
> 
> STRAßE
> 
> STRAẞE
> 
> Straße
> 
> 
> 
> # case insensitive match with casefolding:
> 
> >>> for m in regex.findall(ur"(?if)Straße", u" STRAßE STRASSE STRAẞE Strasse Straße "): print m
> 
> ...
> 
> STRAßE
> 
> STRASSE
> 
> STRAẞE
> 
> Strasse
> 
> Straße
> 
> >>>
> 
> >>>
> 
> 
> 
> # 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ße", u" STRAßE STRASSE STRAẞE Strasse Straße "): print m
> 
> ...
> 
> STRAßE
> 
> STRASSE
> 
> STRAẞE
> 
> Strasse
> 
> Straße
> 
> >>>
> 
> 
> 
> 
> 
> 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:']
> 

------

Vlastimil:

Excellent.

-----

Steven:

..." It's in Unicode, but few fonts show it:" ...

Das grosse Eszett is a member of the unicode subsets MES-2, WGL-4.
Good - serious - fonts are via OpenType MES-2 or WGL-4 compliant.
So, it is a no problem.

I do not know (and I did not check) if the code point, 1e9e, is part of
the utf32 table.

jmf

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


Thread

re: ignore case only for a part of the regex? Helmut Jarausch <jarausch@skynet.be> - 2012-12-30 11:24 +0000
  Re: ignore case only for a part of the regex? Roy Smith <roy@panix.com> - 2012-12-30 10:20 -0500
    Re: ignore case only for a part of the regex? Joel Goldstick <joel.goldstick@gmail.com> - 2012-12-30 10:41 -0500
    Re: ignore case only for a part of the regex? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-01 04:14 +0000
      Re: ignore case only for a part of the regex? Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-01-02 00:09 +0100
        Re: ignore case only for a part of the regex? wxjmfauth@gmail.com - 2013-01-02 06:17 -0800
        Re: ignore case only for a part of the regex? wxjmfauth@gmail.com - 2013-01-02 06:17 -0800
  Re: ignore case only for a part of the regex? Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-12-30 17:38 +0100
    Re: ignore case only for a part of the regex? Roy Smith <roy@panix.com> - 2012-12-30 12:32 -0500
      Re: ignore case only for a part of the regex? MRAB <python@mrabarnett.plus.com> - 2012-12-30 18:04 +0000
      Re: ignore case only for a part of the regex? Cameron Simpson <cs@zip.com.au> - 2012-12-31 11:29 +1100

csiph-web