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


Groups > comp.lang.python > #74036

Re: Question about metacharacter '*'

X-Received by 10.58.94.166 with SMTP id dd6mr11758252veb.12.1404668304119; Sun, 06 Jul 2014 10:38:24 -0700 (PDT)
X-Received by 10.182.66.131 with SMTP id f3mr10559obt.25.1404668303888; Sun, 06 Jul 2014 10:38:23 -0700 (PDT)
Path csiph.com!usenet.pasdenom.info!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!i13no6455907qae.1!news-out.google.com!bp9ni2744igb.0!nntp.google.com!uq10no2137144igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Sun, 6 Jul 2014 10:38:23 -0700 (PDT)
In-Reply-To <roy-279E10.12473806072014@news.panix.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=70.196.68.8; posting-account=h3aEwQoAAACiuqX-oR3gvCVFm8lLHoWj
NNTP-Posting-Host 70.196.68.8
References <3f7ecf04-b881-4e79-aa59-893580090468@googlegroups.com> <CABicbJKUOsKW77LGkDF-1E52AmsPpTJq=GAUTA2PLpff+7fxNQ@mail.gmail.com> <53B96C0A.3030302@mrabarnett.plus.com> <mailman.11545.1404662273.18130.python-list@python.org> <d8f8d76d-0a47-4f59-8f09-da2a44cc1d2e@googlegroups.com> <roy-279E10.12473806072014@news.panix.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <2f63eeac-77c3-48fc-b4dd-020ad74cd0b1@googlegroups.com> (permalink)
Subject Re: Question about metacharacter '*'
From Rick Johnson <rantingrickjohnson@gmail.com>
Injection-Date Sun, 06 Jul 2014 17:38:23 +0000
Content-Type text/plain; charset=ISO-8859-1
X-Received-Bytes 3651
X-Received-Body-CRC 3735597877
Xref csiph.com comp.lang.python:74036

Show key headers only | View raw


On Sunday, July 6, 2014 11:47:38 AM UTC-5, Roy Smith wrote:
> Even better, r"\d+"
> >>> re.search(r'(\d\d*)', '111aaa222').groups()
> ('111',)
> >>> re.search(r'(\d+)', '111aaa222').groups()
> ('111',)

Yes, good catch! I had failed to reduce your original
pattern down to it's most fundamental aspects for the sake
of completeness, and instead, opted to modify it in a manner
that mirrored your example. 

> Oddly enough, I prefer character sets to the backslash
> notation, but I suppose that's largely because when I
> first learned regexes, that new-fangled backslash stuff
> hadn't been invented yet. :-) 

Ha, point taken! :-)

Character sets really shine when you need a fixed range of
letters or numbers which are NOT defined by one of the
"special characters" of \d \D \W \w, etc... 

Say you want to match any letters between "c" and "m" or the
digits between "3" and "6". Defining that pattern using OR'd
"char literals" would be a massive undertaking!

Another great use of character sets is skipping chars that
don't match a "target". For instance, a python comment will
start with one hash char and proceedeth to the end of the
line,,, which when accounting for leading white-space,,,
could be defined by the pattern:

    r'\s*#[^\n]'
    
> Regex is also not as easy to use in Python as it is in a
> language like Perl where it's baked into the syntax.  As a
> result, pythonistas tend to shy away from regex, and
> either never learn the full power, or let their skills
> grow rusty. Which is a shame, because for many tasks,
> there's no better tool.

Agreed, but unfortunately like many other languages, Python
has decided to import all the illogical of regex syntax from
other languages instead of creating a "new" regex syntax
that is consistent and logical. They did the same thing with
Tkinter, and what a nightmare!

And don't misunderstand my statements, i don't intend that
we should create a syntax of verbosity, NO, we *CAN* keep
the syntax succinct whist eliminating the illogical and
inconsistent aspects that plague our patterns.  

Will regex ever be easy to learn, probably not, but they can
be easier to use if only we put on our "big boy" pants and
decide to do something about it!

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


Thread

Question about metacharacter '*' rxjwg98@gmail.com - 2014-07-06 04:51 -0700
  Re: Question about metacharacter '*' Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-07-06 05:09 -0700
    Re: Question about metacharacter '*' rxjwg98@gmail.com - 2014-07-07 11:51 -0700
      Re: Question about metacharacter '*' Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-07-07 13:27 -0700
      Re: Question about metacharacter '*' Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-07 22:50 +0100
  Re: Question about metacharacter '*' MRAB <python@mrabarnett.plus.com> - 2014-07-06 16:32 +0100
  Re: Question about metacharacter '*' Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-07-06 08:50 -0700
    Re: Question about metacharacter '*' Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 09:24 -0700
      Re: Question about metacharacter '*' Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 09:32 -0700
      Re: Question about metacharacter '*' Roy Smith <roy@panix.com> - 2014-07-06 12:47 -0400
        Re: Question about metacharacter '*' Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 10:38 -0700
          Re: Question about metacharacter '*' Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 10:58 -0700

csiph-web