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


Groups > comp.lang.python > #7079

Re: how to avoid leading white spaces

From "Octavian Rasnita" <orasnita@gmail.com>
References <BANLkTikjY3U9Y24s-GOEyi8CNqCFLXuG6g@mail.gmail.com><1237a287-10b0-4a2d-ba35-97b5238deda1@n11g2000yqf.googlegroups.com><4de992d7$0$29996$c3e8da3$5496439d@news.astraweb.com><ef48ad50-da06-47a8-978a-47d6f4271e75@d28g2000yqf.googlegroups.com> <ishuip02d4g@news6.newsguy.com>
Subject Re: how to avoid leading white spaces
Date 2011-06-06 11:51 +0300
Newsgroups comp.lang.python
Message-ID <mailman.2485.1307350328.9059.python-list@python.org> (permalink)

Show all headers | View raw


It is not so hard to decide whether using RE is a good thing or not.

When the speed is important and every millisecond counts, RE should be used 
only when there is no other faster way, because usually RE is less faster 
than using other core Perl/Python functions that can do matching and 
replacing.

When the speed is not such a big issue, RE should be used only if it is 
easier to understand and maintain than using the core functions. And of 
course, RE should be used when the core functions cannot do what RE can do.

In Python, the RE syntax is not so short and simple as in Perl, so using RE 
even for very very simple things requires a longer code, so using other core 
functions may appear as a better solution, because the RE version of the 
code is almost never as easy to read as the code that uses other core 
functions (or... for very simple RE, they are probably same as readable).

In Perl, RE syntax is very short and simple, and in some cases it is more 
easier to understand and maintain a code that uses RE than other core 
functions.

For example, if somebody wants to check if the $var variable contains the 
letter "x", a solution without RE in Perl is:

if ( index( $var, 'x' ) >= 0 ) {
    print "ok";
}

while the solution with RE is:

if ( $var =~ /x/ ) {
    print "ok";
}

And it is obviously that the solution that uses RE is shorter and easier to 
read and maintain, beeing also much more flexible.

Of course, sometimes an even better alternative is to use a module from CPAN 
like Regexp::Common that can use RE in a more simple and readable way for 
matching numbers, profanity words, balanced params, programming languages 
comments, IP and MAC addresses, zip codes... or a module like Email::Valid 
for verifying if an email address is correct, because it may be very hard to 
create a RE for matching an email address.

So... just like with Python, there are more ways to do it, but depending on 
the situation, some of them are better than others. :-)

--Octavian

----- Original Message ----- 
From: "Chris Torek" <nospam@torek.net>
Newsgroups: comp.lang.python
To: <python-list@python.org>
Sent: Monday, June 06, 2011 10:11 AM
Subject: Re: how to avoid leading white spaces


> In article 
> <ef48ad50-da06-47a8-978a-47d6f4271e75@d28g2000yqf.googlegroups.com>
> rurpy@yahoo.com <rurpy@yahoo.com> wrote (in part):
> [mass snippage]
>>What I mean is that I see regexes as being an extremely small,
>>highly restricted, domain specific language targeted specifically
>>at describing text patterns.  Thus they do that job better than
>>than trying to describe patterns implicitly with Python code.
>
> Indeed.
>
> Kernighan has often used / supported the idea of "little languages";
> see:
>
>    http://www.princeton.edu/~hos/frs122/precis/kernighan.htm
>
> In this case, regular expressions form a "little language" that is
> quite well suited to some lexical analysis problems.  Since the
> language is (modulo various concerns) targeted at the "right level",
> as it were, it becomes easy (modulo various concerns :-) ) to
> express the desired algorithm precisely yet concisely.
>
> On the whole, this is a good thing.
>
> The trick lies in knowing when it *is* the right level, and how to
> use the language of REs.
>
>>On 06/03/2011 08:05 PM, Steven D'Aprano wrote:
>>> If regexes were more readable, as proposed by Wall, that would go
>>> a long way to reducing my suspicion of them.
>
> "Suspicion" seems like an odd term here.
>
> Still, it is true that something (whether it be use of re.VERBOSE,
> and whitespace-and-comments, or some New and Improved Syntax) could
> help.  Dense and complex REs are quite powerful, but may also contain
> and hide programming mistakes.  The ability to describe what is
> intended -- which may differ from what is written -- is useful.
>
> As an interesting aside, even without the re.VERBOSE flag, one can
> build complex, yet reasonably-understandable, REs in Python, by
> breaking them into individual parts and giving them appropriate
> names.  (This is also possible in perl, although the perl syntax
> makes it less obvious, I think.)
> -- 
> In-Real-Life: Chris Torek, Wind River Systems
> Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
> email: gmail (figure it out)      http://web.torek.net/torek/index.html
>


--------------------------------------------------------------------------------


> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

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


Thread

Re: how to avoid leading white spaces Chris Rebert <clp2@rebertia.com> - 2011-06-01 10:11 -0700
  Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-01 12:39 -0700
    Re: how to avoid leading white spaces Karim <karim.liateni@free.fr> - 2011-06-01 22:34 +0200
    Re: how to avoid leading white spaces Neil Cerutti <neilc@norwich.edu> - 2011-06-02 13:21 +0000
      Re: how to avoid leading white spaces Roy Smith <roy@panix.com> - 2011-06-02 21:57 -0400
        Re: how to avoid leading white spaces MRAB <python@mrabarnett.plus.com> - 2011-06-03 03:41 +0100
        Re: how to avoid leading white spaces Chris Torek <nospam@torek.net> - 2011-06-03 02:58 +0000
          Re: how to avoid leading white spaces Roy Smith <roy@panix.com> - 2011-06-02 23:44 -0400
            Re: how to avoid leading white spaces Chris Angelico <rosuav@gmail.com> - 2011-06-03 13:52 +1000
            Re: how to avoid leading white spaces Chris Angelico <rosuav@gmail.com> - 2011-06-03 13:54 +1000
            Re: how to avoid leading white spaces Chris Torek <nospam@torek.net> - 2011-06-03 04:30 +0000
              Re: how to avoid leading white spaces Nobody <nobody@nowhere.com> - 2011-06-03 14:11 +0100
          Re: how to avoid leading white spaces Nobody <nobody@nowhere.com> - 2011-06-03 14:18 +0100
          Re: how to avoid leading white spaces Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-04 13:41 +1200
            Re: how to avoid leading white spaces Nobody <nobody@nowhere.com> - 2011-06-04 20:44 +0100
          Re: how to avoid leading white spaces Ian <hobson42@gmail.com> - 2011-06-06 22:04 +0100
            Re: how to avoid leading white spaces Chris Torek <nospam@torek.net> - 2011-06-09 02:32 +0000
        Re: how to avoid leading white spaces Thorsten Kampe <thorsten@thorstenkampe.de> - 2011-06-03 10:32 +0200
      Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-03 05:51 -0700
        Re: how to avoid leading white spaces Neil Cerutti <neilc@norwich.edu> - 2011-06-03 13:17 +0000
          Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-03 08:14 -0700
        Re: how to avoid leading white spaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-03 14:25 +0000
          Re: how to avoid leading white spaces "D'Arcy J.M. Cain" <darcy@druid.net> - 2011-06-03 10:58 -0400
          Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-03 12:29 -0700
            Re: how to avoid leading white spaces Neil Cerutti <neilc@norwich.edu> - 2011-06-03 20:49 +0000
              Re: how to avoid leading white spaces Chris Torek <nospam@torek.net> - 2011-06-03 21:45 +0000
                Re: how to avoid leading white spaces Ethan Furman <ethan@stoneleaf.us> - 2011-06-03 15:11 -0700
                Re: how to avoid leading white spaces MRAB <python@mrabarnett.plus.com> - 2011-06-03 23:38 +0100
                Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-05 22:47 -0700
              Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-05 22:44 -0700
                Re: how to avoid leading white spaces Neil Cerutti <neilc@norwich.edu> - 2011-06-06 16:08 +0000
                Re: how to avoid leading white spaces Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 10:29 -0600
                Re: how to avoid leading white spaces Neil Cerutti <neilc@norwich.edu> - 2011-06-06 17:17 +0000
                Re: how to avoid leading white spaces Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 11:40 -0600
                Re: how to avoid leading white spaces Neil Cerutti <neilc@norwich.edu> - 2011-06-06 17:56 +0000
                Re: how to avoid leading white spaces Ethan Furman <ethan@stoneleaf.us> - 2011-06-06 10:48 -0700
                Re: how to avoid leading white spaces Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 11:42 -0600
            Re: how to avoid leading white spaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-04 02:05 +0000
              Re: how to avoid leading white spaces MRAB <python@mrabarnett.plus.com> - 2011-06-04 03:24 +0100
                Re: how to avoid leading white spaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-04 04:59 +0000
              Re: how to avoid leading white spaces Roy Smith <roy@panix.com> - 2011-06-03 22:30 -0400
                Re: how to avoid leading white spaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-04 05:14 +0000
                Re: how to avoid leading white spaces Roy Smith <roy@panix.com> - 2011-06-04 09:39 -0400
                Re: how to avoid leading white spaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-05 00:44 +0000
                Re: how to avoid leading white spaces rusi <rustompmody@gmail.com> - 2011-06-04 09:36 -0700
                Re: how to avoid leading white spaces Nobody <nobody@nowhere.com> - 2011-06-04 21:02 +0100
                Re: how to avoid leading white spaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-05 01:01 +0000
                Re: how to avoid leading white spaces Chris Angelico <rosuav@gmail.com> - 2011-06-04 16:04 +1000
              Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-05 23:03 -0700
                Re: how to avoid leading white spaces Chris Torek <nospam@torek.net> - 2011-06-06 07:11 +0000
                Re: how to avoid leading white spaces "Octavian Rasnita" <orasnita@gmail.com> - 2011-06-06 11:51 +0300
                Re: how to avoid leading white spaces Chris Angelico <rosuav@gmail.com> - 2011-06-06 19:01 +1000
                Re: how to avoid leading white spaces rusi <rustompmody@gmail.com> - 2011-06-06 07:33 -0700
                Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-07 11:37 -0700
                Re: how to avoid leading white spaces Roy Smith <roy@panix.com> - 2011-06-07 20:30 -0400
                Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-08 07:38 -0700
                Re: how to avoid leading white spaces rusi <rustompmody@gmail.com> - 2011-06-08 09:14 -0700
                Re: how to avoid leading white spaces rusi <rustompmody@gmail.com> - 2011-06-08 01:27 -0700
                Re: how to avoid leading white spaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-06 15:29 +0000
                Re: how to avoid leading white spaces Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 10:06 -0600
                Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-07 09:00 -0700
                Re: how to avoid leading white spaces Duncan Booth <duncan.booth@invalid.invalid> - 2011-06-08 09:01 +0000
                Re: how to avoid leading white spaces "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-06-08 07:39 -0700
          Re: how to avoid leading white spaces rusi <rustompmody@gmail.com> - 2011-06-05 04:17 -0700

csiph-web