[Tutor] Re: print out lines that start with a word
cyresse at gmail.com
Wed Feb 9 10:42:34 CET 2005
>>> x= ["Dude", 'How is it going man', ' ']
>>> print x
['Dude', 'How is it going man', ' ']
>>> for item in x:
... if re.search('\S+',item):
>>> print j
['Dude', 'How is it going man']
Now, I first did that in a Perl script, but it easily comes across to
Python. \S+ will usually mean 'one or more non-whitespace characters'.
Brilliant for when your first Perl script accidentally appended \n to
everything, even \n and \t. *embarassed*
Whereas, while I'm sure there is a string method that can do that, I'm
not sure what it is.
Also - say you have a list of words -
j = " bag apple tomato cheese *marmite* sausages *scones*"
and you wanted to pick up each word that was asterisked.
I did this as a string method then a regex.
... lastIndex = 0
... while 1:
... x = j.index("*", lastIndex + 1)
... lastIndex = x
... except ValueError:
>>> print indexes
>>>myString = j[5:10]
Now the regular expression -
>>> x = re.finditer("\*(?P<myString>.*?)\*", j)
>>> a = x.next()
>>> print a.group('myString')
Now, while the regEx syntax is a big harsh to begin with, once you get
the hang of it, it's OK, and the string method version I used felt too
'hacky' for me. Of course, both only work with pairs of asterisks, so
I guess that makes them both hacky. : )
That's my 2c, I use string methods for stuff like that .startswith, .endswith,
if 'foo' in x stuff is good as well. But sometimes, a regex is the
right tool for the job.
PS Pierre, my wife asked if you could write something in French so
that she could try and read it, as she's trying to pick up her French
again. If you don't mind.
On Wed, 09 Feb 2005 09:44:15 +0100, Pierre Barbier de Reuille
<pierre.barbier at cirad.fr> wrote:
> Wolfram Kraus a écrit :
> > Liam Clarke wrote:
> >> regexes are common across a lot of languages, even Java has them.
> >> Though the pain that would be I daren't not imagine. So the syntax is
> >> familiar, whereas string methods may not be.
> > But IMHO string methods are (more) explicit and readable, the name tells
> > you what the method is doing. I know that sometimes regexp are really
> > fine, e.g. extracting something from html or maybe speed issues (can
> > anyone enlighten me on that one?), but for simple task like the OP's
> > problem I'd always use string methods.
> > Wolfram
> I completely agree ! Then, it will very probably be more efficient. And
> the methods (or functions) like "startwith" are avalaible in almost
> every string library !
> Pierre Barbier de Reuille
> INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
> Botanique et Bio-informatique de l'Architecture des Plantes
> TA40/PSII, Boulevard de la Lironde
> 34398 MONTPELLIER CEDEX 5, France
> tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68
> Tutor maillist - Tutor at python.org
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
More information about the Tutor