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


Groups > comp.lang.python > #98121 > unrolled thread

Regular expressions

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2015-11-02 20:09 -0500
Last post2015-11-03 22:15 +0000
Articles 20 on this page of 106 — 30 participants

Back to article view | Back to comp.lang.python


Contents

  Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-02 20:09 -0500
    Re: Regular expressions MRAB <python@mrabarnett.plus.com> - 2015-11-03 01:19 +0000
      Re: Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-02 22:17 -0500
    Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-02 20:42 -0600
      Re: Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-02 22:17 -0500
        Re: Regular expressions Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-02 22:58 -0500
          Re: Regular expressions rurpy@yahoo.com - 2015-11-02 20:23 -0800
            Re: Regular expressions Michael Torrie <torriem@gmail.com> - 2015-11-02 21:38 -0700
              Re: Regular expressions rurpy@yahoo.com - 2015-11-03 16:33 -0800
                Re: Regular expressions Michael Torrie <torriem@gmail.com> - 2015-11-03 19:04 -0700
                  Re: Regular expressions Dan Sommers <dan@tombstonezero.net> - 2015-11-04 02:55 +0000
                    Re: Regular expressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-11-04 14:23 +1100
                      Re: Regular expressions Michael Torrie <torriem@gmail.com> - 2015-11-03 20:47 -0700
                        Re: Regular expressions Grant Edwards <invalid@invalid.invalid> - 2015-11-04 13:27 +0000
                      Re: Regular expressions Nobody <nobody@nowhere.invalid> - 2015-11-04 05:05 +0000
                      Re: Regular expressions Peter Otten <__peter__@web.de> - 2015-11-04 09:57 +0100
                        Re: Regular expressions Steven D'Aprano <steve@pearwood.info> - 2015-11-05 13:28 +1100
                          Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-04 20:48 -0600
                          Re: Regular expressions Ben Finney <ben+python@benfinney.id.au> - 2015-11-05 14:03 +1100
                          Re: Regular expressions Peter Otten <__peter__@web.de> - 2015-11-05 09:33 +0100
                            Re: Regular expressions Steven D'Aprano <steve@pearwood.info> - 2015-11-05 23:05 +1100
                              Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-05 08:00 -0600
                          Re: Regular expressions Albert van der Horst <albert@spenarnc.xs4all.nl> - 2015-11-05 13:39 +0000
                      Re: Regular expressions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-04 08:00 -0500
                      Re: Regular expressions Michael Torrie <torriem@gmail.com> - 2015-11-04 08:13 -0700
                        Re: Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-04 18:00 -0500
                          Re: Regular expressions rurpy@yahoo.com - 2015-11-04 16:24 -0800
                            Re: Regular expressions Steven D'Aprano <steve@pearwood.info> - 2015-11-05 13:24 +1100
                              Re: Regular expressions rurpy@yahoo.com - 2015-11-04 21:59 -0800
                                Re: Regular expressions Christian Gollwitzer <auriocus@gmx.de> - 2015-11-05 09:18 +0100
                                  Re: Regular expressions rurpy@yahoo.com - 2015-11-06 11:52 -0800
                                    Re: Regular expressions Christian Gollwitzer <auriocus@gmx.de> - 2015-11-06 21:36 +0100
                                      Re: Regular expressions Larry Martell <larry.martell@gmail.com> - 2015-11-06 15:42 -0500
                            Re: Regular expressions Chris Angelico <rosuav@gmail.com> - 2015-11-05 11:34 +1100
                              Re: Regular expressions rurpy@yahoo.com - 2015-11-04 22:27 -0800
                      Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-04 09:42 -0600
                        Re: Regular expressions Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-11-05 20:55 +1300
                          Re: Regular expressions Chris Angelico <rosuav@gmail.com> - 2015-11-05 19:06 +1100
                      What does “grep” stand for? (was: Regular expressions) Ben Finney <ben+python@benfinney.id.au> - 2015-11-05 05:24 +1100
                        Re: What does “grep” stand for? Christian Gollwitzer <auriocus@gmx.de> - 2015-11-04 20:38 +0100
                          Re: What does “grep” stand for? Chris Angelico <rosuav@gmail.com> - 2015-11-05 11:42 +1100
                            Re: What does “grep” stand for? Christian Gollwitzer <auriocus@gmx.de> - 2015-11-05 08:32 +0100
                              Re: What does “grep” stand for? Chris Angelico <rosuav@gmail.com> - 2015-11-05 19:00 +1100
                          Re: What does “grep” stand for? Random832 <random832@fastmail.com> - 2015-11-05 10:19 -0500
                            Re: What does “grep” stand for? Grant Edwards <invalid@invalid.invalid> - 2015-11-05 18:29 +0000
                              Re: What does “grep” stand for? Random832 <random832@fastmail.com> - 2015-11-05 14:56 -0500
                                Re: What does “grep” stand for? Grant Edwards <invalid@invalid.invalid> - 2015-11-05 20:19 +0000
                                  Re: What does “grep” stand for? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-05 20:18 -0500
                                    Re: What does “grep” stand for? Larry Hudson <orgnut@yahoo.com> - 2015-11-05 19:36 -0800
                                      Re: What does “grep” stand for? Dan Sommers <dan@tombstonezero.net> - 2015-11-06 05:31 +0000
                                      Re: What does “grep” stand for? William Ray Wing <wrw@mac.com> - 2015-11-06 08:25 -0500
                                        Re: What does “grep” stand for? Larry Hudson <orgnut@yahoo.com> - 2015-11-06 19:21 -0800
                                    Re: What does ???grep??? stand for? Grant Edwards <invalid@invalid.invalid> - 2015-11-06 14:15 +0000
                                      Re: What does ???grep??? stand for? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-06 20:03 -0500
                      Re: What does “grep” stand for? (was: Regular expressions) Tim Chase <python.list@tim.thechases.com> - 2015-11-04 13:05 -0600
                      Re: Regular expressions Terry Reedy <tjreedy@udel.edu> - 2015-11-04 18:08 -0500
                        Re: Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-04 18:29 -0500
                Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-03 21:12 -0600
                Re: Regular expressions Chris Angelico <rosuav@gmail.com> - 2015-11-04 14:26 +1100
                Re: Regular expressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-11-04 14:48 +1100
                  Re: Regular expressions Christian Gollwitzer <auriocus@gmx.de> - 2015-11-04 08:21 +0100
                    Re: Regular expressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-11-04 19:47 +1100
                      Re: Regular expressions rurpy@yahoo.com - 2015-11-04 06:43 -0800
                  Re: Regular expressions rurpy@yahoo.com - 2015-11-04 06:38 -0800
                    Re: Regular expressions Chris Angelico <rosuav@gmail.com> - 2015-11-05 01:52 +1100
                      Re: Regular expressions rurpy@yahoo.com - 2015-11-04 16:13 -0800
                        Re: Regular expressions Chris Angelico <rosuav@gmail.com> - 2015-11-05 11:33 +1100
                          Re: Regular expressions rurpy@yahoo.com - 2015-11-04 21:42 -0800
                        Re: Regular expressions Steven D'Aprano <steve@pearwood.info> - 2015-11-05 13:26 +1100
                          Re: Regular expressions Ben Finney <ben+python@benfinney.id.au> - 2015-11-05 14:07 +1100
                          Re: Regular expressions rurpy@yahoo.com - 2015-11-04 21:54 -0800
                        Re: Regular expressions Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-05 10:14 +0100
                  Re: Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-04 18:02 -0500
                    Re: Regular expressions Steven D'Aprano <steve@pearwood.info> - 2015-11-05 11:54 +1100
                      Re: Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-05 10:07 -0500
                        Re: Regular expressions rurpy@yahoo.com - 2015-11-06 12:46 -0800
            Re: Regular expressions Steven D'Aprano <steve@pearwood.info> - 2015-11-03 18:15 +1100
              Re: Regular expressions Nick Sarbicki <nick.a.sarbicki@gmail.com> - 2015-11-03 08:43 +0000
              Re: Regular expressions rurpy@yahoo.com - 2015-11-03 16:22 -0800
        Re: Regular expressions Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-03 12:38 +0000
        Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-03 05:53 -0600
        Re: Regular expressions Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-03 10:34 -0500
          Re: Regular expressions Seymore4Head <Seymore4Head@Hotmail.invalid> - 2015-11-03 11:10 -0500
            Re: Regular expressions Chris Angelico <rosuav@gmail.com> - 2015-11-04 03:20 +1100
              Re: Regular expressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-11-04 14:35 +1100
                Re: Regular expressions Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-04 12:41 +0100
      Re: Regular expressions Grant Edwards <invalid@invalid.invalid> - 2015-11-03 14:56 +0000
    Re: Regular expressions Michael Torrie <torriem@gmail.com> - 2015-11-02 20:51 -0700
      Re: Regular expressions rurpy@yahoo.com - 2015-11-02 20:23 -0800
        Re: Regular expressions Michael Torrie <torriem@gmail.com> - 2015-11-02 21:33 -0700
        Re: Regular expressions Robin Koch <robin.koch@t-online.de> - 2015-11-03 23:58 +0100
    Re: Regular expressions Peter Otten <__peter__@web.de> - 2015-11-03 10:25 +0100
    Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-03 05:50 -0600
    Re: Regular expressions Peter Otten <__peter__@web.de> - 2015-11-03 15:00 +0100
      Re: Regular expressions Jussi Piitulainen <harvesting@makes.email.invalid> - 2015-11-03 17:12 +0200
        Irregular last line in a text file, was Re: Regular expressions Peter Otten <__peter__@web.de> - 2015-11-03 16:35 +0100
          Re: Irregular last line in a text file, was Re: Regular expressions Jussi Piitulainen <harvesting@makes.email.invalid> - 2015-11-03 18:42 +0200
        Re: Irregular last line in a text file, was Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-03 10:56 -0600
          Re: Irregular last line in a text file, was Re: Regular expressions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-11-04 14:39 +1100
            Re: Irregular last line in a text file, was Re: Regular expressions Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-11-04 10:07 +0000
            Re: Irregular last line in a text file, was Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-04 09:33 -0600
        Re: Irregular last line in a text file, was Re: Regular expressions Peter Otten <__peter__@web.de> - 2015-11-03 18:44 +0100
        Re: Irregular last line in a text file, was Re: Regular expressions Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-03 11:33 -0700
        Re: Irregular last line in a text file, was Re: Regular expressions Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-03 11:39 -0700
        Re: Irregular last line in a text file, was Re: Regular expressions Tim Chase <python.list@tim.thechases.com> - 2015-11-03 13:45 -0600
          Re: Irregular last line in a text file, was Re: Regular expressions Grant Edwards <invalid@invalid.invalid> - 2015-11-03 22:15 +0000

Page 5 of 6 — ← Prev page 1 2 3 4 [5] 6  Next page →


#98158

FromTim Chase <python.list@tim.thechases.com>
Date2015-11-03 05:53 -0600
Message-ID<mailman.25.1446560274.8789.python-list@python.org>
In reply to#98124
On 2015-11-02 22:17, Seymore4Head wrote:
> On Mon, 2 Nov 2015 20:42:37 -0600, Tim Chase
> <python.list@tim.thechases.com> wrote:
> 
> >On 2015-11-02 20:09, Seymore4Head wrote:
> >> How do I make a regular expression that returns true if the end
> >> of the line is an asterisk
> >
> >Why use a regular expression?
> >
> Because that is the part of Python I am trying to learn at the
> moment. Thanks

Ah, well that's an entirely different problem-space, so then you
would want to use MRAB's answer

  r = re.compile(r"\*$")

-tkc


[toc] | [prev] | [next] | [standalone]


#98164

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2015-11-03 10:34 -0500
Message-ID<mailman.28.1446564855.8789.python-list@python.org>
In reply to#98124
On Mon, Nov 2, 2015 at 10:17 PM, Seymore4Head <Seymore4Head@hotmail.invalid>
wrote:

> On Mon, 2 Nov 2015 20:42:37 -0600, Tim Chase
> <python.list@tim.thechases.com> wrote:
>
> >On 2015-11-02 20:09, Seymore4Head wrote:
> >> How do I make a regular expression that returns true if the end of
> >> the line is an asterisk
> >
> >Why use a regular expression?
> >
> >  if line[-1] == '*':
> >    yep(line)
> >  else:
> >    nope(line)
> >
> >-tkc
> >
> >
> Because that is the part of Python I am trying to learn at the moment.
>

Are we to infer that you were aware of doing the   if line[-1] == '*': ...
, but just wanted to learn how to do the same thing with regex? Or that you
heard about regexes and thought that would be the way to solve your puzzle?

> Thanks
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#98170

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2015-11-03 11:10 -0500
Message-ID<tjmh3blsb60fmvginp4q6sbe2659p4rh9f@4ax.com>
In reply to#98164
On Tue, 3 Nov 2015 10:34:12 -0500, Joel Goldstick
<joel.goldstick@gmail.com> wrote:

>On Mon, Nov 2, 2015 at 10:17 PM, Seymore4Head <Seymore4Head@hotmail.invalid>
>wrote:
>
>> On Mon, 2 Nov 2015 20:42:37 -0600, Tim Chase
>> <python.list@tim.thechases.com> wrote:
>>
>> >On 2015-11-02 20:09, Seymore4Head wrote:
>> >> How do I make a regular expression that returns true if the end of
>> >> the line is an asterisk
>> >
>> >Why use a regular expression?
>> >
>> >  if line[-1] == '*':
>> >    yep(line)
>> >  else:
>> >    nope(line)
>> >
>> >-tkc
>> >
>> >
>> Because that is the part of Python I am trying to learn at the moment.
>>
>
>Are we to infer that you were aware of doing the   if line[-1] == '*': ...
>, but just wanted to learn how to do the same thing with regex? Or that you
>heard about regexes and thought that would be the way to solve your puzzle?
>
>> Thanks
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
Yes I knew that -1 represents the end character.  It is not a question
of trying to accomplish anything.  I was just practicing with regex
and wasn't sure how to express a * since it was one of the
instructions.

[toc] | [prev] | [next] | [standalone]


#98171

FromChris Angelico <rosuav@gmail.com>
Date2015-11-04 03:20 +1100
Message-ID<mailman.34.1446567607.8789.python-list@python.org>
In reply to#98170
On Wed, Nov 4, 2015 at 3:10 AM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> Yes I knew that -1 represents the end character.  It is not a question
> of trying to accomplish anything.  I was just practicing with regex
> and wasn't sure how to express a * since it was one of the
> instructions.

In that case, it's nothing to do with ending a string. What you really
want to know is: How do you match a '*' using a regular expression?
Which is what MRAB answered, courtesy of a working crystal ball: You
use '\*'. Everything about the end of the string is irrelevant. (So,
too, are all the comments about using [-1] or string methods. But we
weren't to know that.)

ChrisA

[toc] | [prev] | [next] | [standalone]


#98204

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-11-04 14:35 +1100
Message-ID<56397cfd$0$1601$c3e8da3$5496439d@news.astraweb.com>
In reply to#98171
On Wednesday 04 November 2015 03:20, Chris Angelico wrote:

> On Wed, Nov 4, 2015 at 3:10 AM, Seymore4Head
> <Seymore4Head@hotmail.invalid> wrote:
>> Yes I knew that -1 represents the end character.  It is not a question
>> of trying to accomplish anything.  I was just practicing with regex
>> and wasn't sure how to express a * since it was one of the
>> instructions.
> 
> In that case, it's nothing to do with ending a string. 

Seymore never said anything about ending a string.


> What you really
> want to know is: How do you match a '*' using a regular expression?

He may want to know that too, but that's not what he asked for. He asked how 
to match an asterisk at the end of the line.


> Which is what MRAB answered, courtesy of a working crystal ball: You
> use '\*'. Everything about the end of the string is irrelevant.

Not at all -- matching "\*" will find lines *beginning* with an asterisk if 
you use re.match, and lines containing an asterisk *anywhere* in the line if 
you use re.search.

I say "line" because the most common use for re.match and re.search is to 
match against a single line of text, but of course regexes can operate on 
multiline blocks of text, with or without multiline mode turned on.

And calling it "a working crystal ball" is somewhat of an exaggeration. The 
plain English meaning of Seymore's plain English question is easily 
understood: he wants to know how to match an asterisk at the end of the 
line, just like he said :-P


> (So,
> too, are all the comments about using [-1] or string methods. But we
> weren't to know that.)

If MRAB could understand what he wanted, I'm sure most others could have 
too.



-- 
Steve

[toc] | [prev] | [next] | [standalone]


#98220

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2015-11-04 12:41 +0100
Message-ID<mailman.11.1446637375.16136.python-list@python.org>
In reply to#98204
Op 04-11-15 om 04:35 schreef Steven D'Aprano:
> On Wednesday 04 November 2015 03:20, Chris Angelico wrote:
>
>> (So,
>> too, are all the comments about using [-1] or string methods. But we
>> weren't to know that.)
> If MRAB could understand what he wanted, I'm sure most others could have 
> too.

Yes, they were just to busy to try pushing the OP in an other direction, for
them to answer the question.

-- 
Antoon Pardon

[toc] | [prev] | [next] | [standalone]


#98160

FromGrant Edwards <invalid@invalid.invalid>
Date2015-11-03 14:56 +0000
Message-ID<n1ahu0$aur$1@reader1.panix.com>
In reply to#98123
On 2015-11-03, Tim Chase <python.list@tim.thechases.com> wrote:
> On 2015-11-02 20:09, Seymore4Head wrote:
>> How do I make a regular expression that returns true if the end of
>> the line is an asterisk
>
> Why use a regular expression?
>
>   if line[-1] == '*':

Why use a negative index and then a compare?

    if line.endswith('*'):

If you want to know if a string ends with something, just ask it!
    
;)
    
-- 
Grant Edwards               grant.b.edwards        Yow! RELATIVES!!
                                  at               
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#98127

FromMichael Torrie <torriem@gmail.com>
Date2015-11-02 20:51 -0700
Message-ID<mailman.2.1446522680.8789.python-list@python.org>
In reply to#98121
On 11/02/2015 07:42 PM, Tim Chase wrote:
> On 2015-11-02 20:09, Seymore4Head wrote:
>> How do I make a regular expression that returns true if the end of
>> the line is an asterisk
> 
> Why use a regular expression?
> 
>   if line[-1] == '*':
>     yep(line)
>   else:
>     nope(line)

Indeed, sometimes Jamie Zawinski's is often quite appropriate:

    Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.

[toc] | [prev] | [next] | [standalone]


#98129

Fromrurpy@yahoo.com
Date2015-11-02 20:23 -0800
Message-ID<0cb3abb6-ec1e-4357-ab0f-b018d0c688fa@googlegroups.com>
In reply to#98127
On 11/02/2015 08:51 PM, Michael Torrie wrote:
>[...]
> Indeed, sometimes Jamie Zawinski's is often quite appropriate:
> 
>     Some people, when confronted with a problem, think "I know, I'll use
> regular expressions." Now they have two problems.

Or its sometimes heard paraphrase:
  Some people, when confronted with a problem, think "I know, I'll use
  Python." Now they have two problems
The point being it's a cute and memorable aphorism but not very meaningful
because it can be applied to anything one wishes to denigrate.

Of course there are people who misuse regexes. But I am quite sure,
especially in the Python community, there are just as many who fail to
use them when they are appropriate which is just as bad.

[toc] | [prev] | [next] | [standalone]


#98131

FromMichael Torrie <torriem@gmail.com>
Date2015-11-02 21:33 -0700
Message-ID<mailman.4.1446525235.8789.python-list@python.org>
In reply to#98129
On 11/02/2015 09:23 PM, rurpy--- via Python-list wrote:
> On 11/02/2015 08:51 PM, Michael Torrie wrote:
>> [...]
>> Indeed, sometimes Jamie Zawinski's is often quite appropriate:
>>
>>     Some people, when confronted with a problem, think "I know, I'll use
>> regular expressions." Now they have two problems.
> 
> Or its sometimes heard paraphrase:
>   Some people, when confronted with a problem, think "I know, I'll use
>   Python." Now they have two problems
> The point being it's a cute and memorable aphorism but not very meaningful
> because it can be applied to anything one wishes to denigrate.
> 
> Of course there are people who misuse regexes. But I am quite sure,
> especially in the Python community, there are just as many who fail to
> use them when they are appropriate which is just as bad.

Judging by a few posts on the list lately, I'd say it is highly relevant
to Python itself.  Too many people have only a vague notion of a problem
they'd like to solve and although they don't really understand the
problem, they've heard Python is a good language to learn, so they ask
how they can solve that problem with Python.

Now, this certainly can work for a person who's already experienced in
several languages and who already understands the problem.  For others,
it's very much now two intractable problems.

[toc] | [prev] | [next] | [standalone]


#98192

FromRobin Koch <robin.koch@t-online.de>
Date2015-11-03 23:58 +0100
Message-ID<n1be6n$6rf$1@news.albasani.net>
In reply to#98129
Am 03.11.2015 um 05:23 schrieb rurpy@yahoo.com:

> Of course there are people who misuse regexes.

/^1?$|^(11+?)\1+$/

There are? 0:-)

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#98143

FromPeter Otten <__peter__@web.de>
Date2015-11-03 10:25 +0100
Message-ID<mailman.10.1446542744.8789.python-list@python.org>
In reply to#98121
Michael Torrie wrote:

> On 11/02/2015 07:42 PM, Tim Chase wrote:
>> On 2015-11-02 20:09, Seymore4Head wrote:
>>> How do I make a regular expression that returns true if the end of
>>> the line is an asterisk
>> 
>> Why use a regular expression?
>> 
>>   if line[-1] == '*':
>>     yep(line)
>>   else:
>>     nope(line)
> 
> Indeed, sometimes Jamie Zawinski's is often quite appropriate:
> 
>     Some people, when confronted with a problem, think "I know, I'll use
> regular expressions." Now they have two problems.

Incidentally the code example has two "problems", too.

- What about the empty string?
- What about lines with a trailing "\n", i. e. as they are usually delivered
  when iterating over a file?

Below is a comparison of some of your options. The "one obvious way" 
line.rstrip("\n").endswith("*") is not included ;)

$ cat starry_table.py 
import re


def show_table(data, header):
    rows = [header]
    rows.extend([str(c) for c in row] for row in data)
    widths = [max(len(row[i]) for row in rows) for i in range(len(header))]
    template = "  ".join("{:%d}" % w for w in widths)
    for row in rows:
        print(template.format(*row))


def compare(sample_lines):
    for line in sample_lines:
        got_re = bool(re.compile("\*$").search(line))
        got_re_M = bool(re.compile("\*$", re.M).search(line))
        got_endswith = line.endswith("*")
        got_endswith2 = line.endswith(("*", "*\n"))
        got_substring = line[-1:] == "*"
        try:
            got_char = line[-1] == "*"
        except IndexError:
            got_char = "#exception"
        results = (
            got_re, got_re_M,
            got_endswith, got_endswith2,
            got_substring, got_char)
        yield (
            ["", "X"][len(set(results)) > 1],
            repr(line)) + results


SAMPLE = ["", "\n", "foo\n", "*\n", "*", "foo*", "foo*\n", "foo*\nbar"]
HEADER = [
    "", "line", "regex", "re.M",
    "endswith", 'endswith(("*", "*\\n"))',
    "substring", "char"]

if __name__ == "__main__":
    show_table(compare(SAMPLE), HEADER)


$ python3 starry_table.py 
   line         regex  re.M   endswith  endswith(("*", "*\n"))  substring  char      
X  ''           False  False  False     False                   False      #exception
   '\n'         False  False  False     False                   False      False     
   'foo\n'      False  False  False     False                   False      False     
X  '*\n'        True   True   False     True                    False      False     
   '*'          True   True   True      True                    True       True      
   'foo*'       True   True   True      True                    True       True      
X  'foo*\n'     True   True   False     True                    False      False     
X  'foo*\nbar'  False  True   False     False                   False      False     

[toc] | [prev] | [next] | [standalone]


#98147

FromTim Chase <python.list@tim.thechases.com>
Date2015-11-03 05:50 -0600
Message-ID<mailman.15.1446551876.8789.python-list@python.org>
In reply to#98121
On 2015-11-03 10:25, Peter Otten wrote:
> >>> How do I make a regular expression that returns true if the end
> >>> of the line is an asterisk
> >> 
> >> Why use a regular expression?
> >> 
> >>   if line[-1] == '*':
> >>     yep(line)
> >>   else:
> >>     nope(line)
> 
> Incidentally the code example has two "problems", too.
> 
> - What about the empty string?

Good catch: .endswith() works better.

> - What about lines with a trailing "\n", i. e. as they are usually
> delivered when iterating over a file?

Then your string *doesn't* end with a "*", but rather with a
newline. ;-)

Though according to the OP's specs, the following function would work
too:

  def ends_in_asterisk(s):
    return True

It *does* return True if the line ends in an asterisk (no requirement
to make the function return False under any other conditions).

-tkc




[toc] | [prev] | [next] | [standalone]


#98154

FromPeter Otten <__peter__@web.de>
Date2015-11-03 15:00 +0100
Message-ID<mailman.21.1446559261.8789.python-list@python.org>
In reply to#98121
Tim Chase wrote:

> On 2015-11-03 10:25, Peter Otten wrote:
>> >>> How do I make a regular expression that returns true if the end
>> >>> of the line is an asterisk
>> >> 
>> >> Why use a regular expression?
>> >> 
>> >>   if line[-1] == '*':
>> >>     yep(line)
>> >>   else:
>> >>     nope(line)
>> 
>> Incidentally the code example has two "problems", too.
>> 
>> - What about the empty string?
> 
> Good catch: .endswith() works better.
> 
>> - What about lines with a trailing "\n", i. e. as they are usually
>> delivered when iterating over a file?
> 
> Then your string *doesn't* end with a "*", but rather with a
> newline. ;-)
> 
> Though according to the OP's specs, the following function would work
> too:
> 
>   def ends_in_asterisk(s):
>     return True
> 
> It *does* return True if the line ends in an asterisk (no requirement
> to make the function return False under any other conditions).

If a "line" is defined as a string that ends with a newline

def ends_in_asterisk(line):
    return False

would also satisfy the requirement. Lies, damned lies, and specs ;)

[toc] | [prev] | [next] | [standalone]


#98163

FromJussi Piitulainen <harvesting@makes.email.invalid>
Date2015-11-03 17:12 +0200
Message-ID<lf5io5junfn.fsf@ling.helsinki.fi>
In reply to#98154
Peter Otten writes:

> If a "line" is defined as a string that ends with a newline
>
> def ends_in_asterisk(line):
>     return False
>
> would also satisfy the requirement. Lies, damned lies, and specs ;)

Even if a "line" is defined as a string that comes from reading
something like a file with default options, a line may end in
an asterisk.

>>> [ line.endswith('*') for line in StringIO('rivi*\nrivi*\nrivi*') ]
[False, False, True]

[toc] | [prev] | [next] | [standalone]


#98165 — Irregular last line in a text file, was Re: Regular expressions

FromPeter Otten <__peter__@web.de>
Date2015-11-03 16:35 +0100
SubjectIrregular last line in a text file, was Re: Regular expressions
Message-ID<mailman.29.1446564939.8789.python-list@python.org>
In reply to#98163
Jussi Piitulainen wrote:

> Peter Otten writes:
> 
>> If a "line" is defined as a string that ends with a newline
>>
>> def ends_in_asterisk(line):
>>     return False
>>
>> would also satisfy the requirement. Lies, damned lies, and specs ;)
> 
> Even if a "line" is defined as a string that comes from reading
> something like a file with default options, a line may end in
> an asterisk.
 
Note that the last line from the file is not a line as defined by me in the 
above post ;)

>>>> [ line.endswith('*') for line in StringIO('rivi*\nrivi*\nrivi*') ]
> [False, False, True]

I wish there were a way to prohibit such files. Maybe a special value

with open(..., newline="normalize") f: 
    assert all(line.endswith("\n") for line in f)

to ensure that all lines end with "\n"?

[toc] | [prev] | [next] | [standalone]


#98174 — Re: Irregular last line in a text file, was Re: Regular expressions

FromJussi Piitulainen <harvesting@makes.email.invalid>
Date2015-11-03 18:42 +0200
SubjectRe: Irregular last line in a text file, was Re: Regular expressions
Message-ID<lf5bnbbxcer.fsf@ling.helsinki.fi>
In reply to#98165
Peter Otten writes:
> Jussi Piitulainen wrote:
>> Peter Otten writes:
>> 
>>> If a "line" is defined as a string that ends with a newline
>>>
>>> def ends_in_asterisk(line):
>>>     return False
>>>
>>> would also satisfy the requirement. Lies, damned lies, and specs ;)
>> 
>> Even if a "line" is defined as a string that comes from reading
>> something like a file with default options, a line may end in
>> an asterisk.
>  
> Note that the last line from the file is not a line as defined by me
> in the above post ;)

Noted.

>>>>> [ line.endswith('*') for line in StringIO('rivi*\nrivi*\nrivi*') ]
>> [False, False, True]
>
> I wish there were a way to prohibit such files. Maybe a special value
>
> with open(..., newline="normalize") f: 
>     assert all(line.endswith("\n") for line in f)
>
> to ensure that all lines end with "\n"?

I'd like that. It should be the default.

[toc] | [prev] | [next] | [standalone]


#98176 — Re: Irregular last line in a text file, was Re: Regular expressions

FromTim Chase <python.list@tim.thechases.com>
Date2015-11-03 10:56 -0600
SubjectRe: Irregular last line in a text file, was Re: Regular expressions
Message-ID<mailman.37.1446569907.8789.python-list@python.org>
In reply to#98163
On 2015-11-03 16:35, Peter Otten wrote:
> I wish there were a way to prohibit such files. Maybe a special
> value
> 
> with open(..., newline="normalize") f: 
>     assert all(line.endswith("\n") for line in f)
> 
> to ensure that all lines end with "\n"?

Or even more valuable to me:

  with open(..., newline="strip") as f:
    assert all(not line.endswith(("\n", "\r")) for line in f)

because I have countless loops that look something like

  with open(...) as f:
    for line in f:
      line = line.rstrip('\r\n')
      process(line)

-tkc



[toc] | [prev] | [next] | [standalone]


#98205 — Re: Irregular last line in a text file, was Re: Regular expressions

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-11-04 14:39 +1100
SubjectRe: Irregular last line in a text file, was Re: Regular expressions
Message-ID<56397dd9$0$1601$c3e8da3$5496439d@news.astraweb.com>
In reply to#98176
On Wednesday 04 November 2015 03:56, Tim Chase wrote:

> Or even more valuable to me:
> 
>   with open(..., newline="strip") as f:
>     assert all(not line.endswith(("\n", "\r")) for line in f)

# Works only on Windows text files.
def chomp(lines):
    for line in lines:
        yield line.rstrip('\r\n')


Better would be this:

def chomp(lines):
    for line in lines:
        yield line.rstrip()  # remove all trailing whitespace


with open(...) as f:
    for line in chomp(f): ...


-- 
Steve

[toc] | [prev] | [next] | [standalone]


#98216 — Re: Irregular last line in a text file, was Re: Regular expressions

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2015-11-04 10:07 +0000
SubjectRe: Irregular last line in a text file, was Re: Regular expressions
Message-ID<mailman.8.1446631662.16136.python-list@python.org>
In reply to#98205
On 4 November 2015 at 03:39, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>
> Better would be this:
>
> def chomp(lines):
>     for line in lines:
>         yield line.rstrip()  # remove all trailing whitespace
>
>
> with open(...) as f:
>     for line in chomp(f): ...

with open(...) as f:
    for line in map(str.rstrip, f): ...

--
Oscar

[toc] | [prev] | [next] | [standalone]


Page 5 of 6 — ← Prev page 1 2 3 4 [5] 6  Next page →

Back to top | Article view | comp.lang.python


csiph-web