Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [perl-python] string pattern matching

Reply
Thread Tools

[perl-python] string pattern matching

 
 
Xah Lee
Guest
Posts: n/a
 
      02-01-2005
# -*- coding: utf-8 -*-
# Python

# Matching string patterns
#
# Sometimes you want to know if a string is of
# particular pattern. Let's say in your website
# you have converted all images files from gif
# format to png format. Now you need to change the
# html code to use the .png files. So, essentially
# you want to replace strings of the form
#
# img src="*.gif"
# to
# img src="*.png"
#
# Python provides string pattern matching by the
# "re" module. (String Pattern Matching is
# inanely known as Regular Expression (regex) in
# the computing industry. It is a misnomer and
# causes great unnecessary confusion.)


©import re
©
©text='''<img src="../Icons_dir/icon_sum.gif" width="32"
height="32">'''
©
©pattern = r'''src="([^"]+)\.gif"'''
©
©result = re.search(pattern, text)
©
©if result:
© print 'yes'
© print result.expand(r"\1") # the captured pattern
©else:
© print 'no'

#-------------
# if re.search(pattern, text) does not match, then the result is
None. If it matches, an object is returned. One can then use methods
such as .groups(), .expand(), .split() ... to find the matched parts,
or a given replacement string, or split the text into multiple part
by the regex...

# regex is quite confusing for beginners, but
# isn't really difficult to understand. It comes
# with practice.

# see
# http://python.org/doc/lib/module-re.html
# for detail.

# i'll have to study more to make complet example. Try it yourself.

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

# in perl, regex is its mainstay.
# very expressive with syntax, but not so in semantic,
# when compared to Python.

# for syntax variability, for example the following are all the same.

$text = "what ever is here to be matched";

if ( $text =~ ever) { print 'yes'} else {print "no"}
if ( $text =~ /ever/) { print 'yes'} else {print "no";}
if ( $text =~ m/ever/) { print 'yes'} else {print "no"}
if ( $text =~ m(ever)) { print 'yes'} else {print "no"}
if ( $text =~ m@ever@) { print 'yes'} else {print "no"}

# for detail of its much ado about nothing nature,
# see perldoc perlre

--------
this is perl-python a-day mailing list. To subscribe, see
http://xahlee.org/perl-python/python.html
Xah

http://xahlee.org/PageTwo_dir/more.html

 
Reply With Quote
 
 
 
 
Daniel Fackrell
Guest
Posts: n/a
 
      02-01-2005
Perhaps a message to the effect of "These messages are specifically disowned
by the groups to which they are posted, have historically been riddled with
blatant errors, and are assumed to continue in the same quality." should be
posted as a follow-up to each of these messages by XL in order to avoid
having to spend the time to find the inaccuracies in each one individually.

Considering the response so far, a list of frequent posters and
not-so-frequent posters who will vouch for the accuracy of the disclaimer
might even be added.

Daniel Fackrell



 
Reply With Quote
 
 
 
 
Dan Perl
Guest
Posts: n/a
 
      02-01-2005
Perhaps someone will write a program to automatically follow up on every
[perl-python] posting? The follow-up could just contain a statement like
the one Daniel mentions. Obviously the program would be written in python.


Any suggestions on how to implement such a program? How would it detect a
new posting? How would it send the follow-up?

Anyway, I agree with Daniel and I think that would not only warn newcomers
to the group, but it would also allow many of us to move on without worrying
about the effect that the perl-python postings may have on these newcomers.

BTW, I think Daniel's suggestion for the statement sounds pretty fair and
pretty impartial. Maybe it should just not use terms like "blatant". And I
would include something like "we support everyone's freedom to post to this
newsgroup but we feel that python and perl beginners need to be warned about
the quality of these tutorials".

Dan

"Daniel Fackrell" <> wrote in message
news:mailman.1716.1107300060.22381.python-...
> Perhaps a message to the effect of "These messages are specifically
> disowned
> by the groups to which they are posted, have historically been riddled
> with
> blatant errors, and are assumed to continue in the same quality." should
> be
> posted as a follow-up to each of these messages by XL in order to avoid
> having to spend the time to find the inaccuracies in each one
> individually.
>
> Considering the response so far, a list of frequent posters and
> not-so-frequent posters who will vouch for the accuracy of the disclaimer
> might even be added.
>
> Daniel Fackrell
>
>
>



 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      02-02-2005
Dan Perl wrote:

> Perhaps someone will write a program to automatically follow up on every
> [perl-python] posting? The follow-up could just contain a statement like
> the one Daniel mentions. Obviously the program would be written in python.
>


I'm not really sure that such a disclaimer is explicitly necessary.
Anyone looking at Xah Lee's posts will also see the threads they
generate, which involve people pointing out all their errors. Granted
this won't happen with every single post, but since he's posting this
stuff once a day, I don't think the chances of someone finding his posts
and not seeing the related discussion and refutations is a big risk.

For the rest of us, we can just killfile the threads easily enough.

--
Erik Max Francis && && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Little things / Cut like knives / Hurt and sting
-- Anggun
 
Reply With Quote
 
Stephen Thorne
Guest
Posts: n/a
 
      02-02-2005
On Tue, 1 Feb 2005 18:59:18 -0500, Dan Perl <> wrote:
> Perhaps someone will write a program to automatically follow up on every
> [perl-python] posting? The follow-up could just contain a statement like
> the one Daniel mentions. Obviously the program would be written in python.
>
>
> Any suggestions on how to implement such a program? How would it detect a
> new posting? How would it send the follow-up?
>
> Anyway, I agree with Daniel and I think that would not only warn newcomers
> to the group, but it would also allow many of us to move on without worrying
> about the effect that the perl-python postings may have on these newcomers.


I'd just like the python- mailing list to drop his
posts on the floor so I don't have to read them.

But thats me.

Stephen.
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      02-02-2005
>>>>> Stephen Thorne <> writes:

> On Tue, 1 Feb 2005 18:59:18 -0500, Dan Perl <> wrote:
>> Perhaps someone will write a program to automatically follow up
>> on every [perl-python] posting? The follow-up could just
>> contain a statement like the one Daniel mentions. Obviously
>> the program would be written in python.
>>
>> Any suggestions on how to implement such a program? How would
>> it detect a new posting? How would it send the follow-up?
>>
>> Anyway, I agree with Daniel and I think that would not only
>> warn newcomers to the group, but it would also allow many of us
>> to move on without worrying about the effect that the
>> perl-python postings may have on these newcomers.


> I'd just like the python- mailing list to drop
> his posts on the floor so I don't have to read them.


> But thats me.


> Stephen.


Falls into the 'cure worse than the disease' category.
It's really just a prompt to explore the corners of Gnus, and
determine how to give X.L. the thorough ignoring he deserves.
R,
C
 
Reply With Quote
 
Stephen Thorne
Guest
Posts: n/a
 
      02-02-2005
On Tue, 01 Feb 2005 21:19:34 -0500, Chris Smith
<> wrote:
> Falls into the 'cure worse than the disease' category.
> It's really just a prompt to explore the corners of Gnus, and
> determine how to give X.L. the thorough ignoring he deserves.


*headdesk*

I'm using gmail, and I can set up the filter trivially
(from -> delete), but I just wasn't thinking clearly. I
was cursing not having the ability to use a procmail filter while the
solution was right in front of me.

Regards,
Stephen.
 
Reply With Quote
 
Daniel Fackrell
Guest
Posts: n/a
 
      02-02-2005
- Henry Wadsworth Longfellow
"Erik Max Francis" <> wrote in message
news:fbydnQJ8DNSthJ3fRVn-...
> Dan Perl wrote:
>
> > Perhaps someone will write a program to automatically follow up on every
> > [perl-python] posting? The follow-up could just contain a statement

like
> > the one Daniel mentions. Obviously the program would be written in

python.
> >

>
> I'm not really sure that such a disclaimer is explicitly necessary.
> Anyone looking at Xah Lee's posts will also see the threads they
> generate, which involve people pointing out all their errors. Granted
> this won't happen with every single post, but since he's posting this
> stuff once a day, I don't think the chances of someone finding his posts
> and not seeing the related discussion and refutations is a big risk.
>
> For the rest of us, we can just killfile the threads easily enough.


It seems to me that application of one of these solutions reduces the
effectiveness of the other. If enough persons killfile the threads, who
warns the newbies? And so those who don't killfile the threads to ensure
that somebody is still guarding against misleading information to newbies
continue dealing with it manually.

Daniel Fackrell



 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      02-02-2005
Daniel Fackrell wrote:

> It seems to me that application of one of these solutions reduces the
> effectiveness of the other. If enough persons killfile the threads, who
> warns the newbies? And so those who don't killfile the threads to ensure
> that somebody is still guarding against misleading information to newbies
> continue dealing with it manually.


My point was, a sufficiently persistent pest will always generate enough
dissatisfaction to get people pointing out his uselessness. Even when
others killfile him, there will be those that stick around to point out
his foolishness to others, including any newbies who might otherwise
conclude that Xah Lee knows what the hell he's talking about.

And, in fact, that's exactly what we're seeing. It just seems to me
that any further institutionalization of criticism of Xah Lee's posts is
unnecessary; it's already being handled, at low levels of annoyance that
can be avoided by anyone with a killfile or mail filter.

--
Erik Max Francis && && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Divorces are made in Heaven.
-- Oscar Wilde
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      02-02-2005
>>>>> Stephen Thorne <> writes:

> On Tue, 01 Feb 2005 21:19:34 -0500, Chris Smith
> <> wrote:
>> Falls into the 'cure worse than the disease' category. It's
>> really just a prompt to explore the corners of Gnus, and
>> determine how to give X.L. the thorough ignoring he deserves.


> *headdesk*


> I'm using gmail, and I can set up the filter trivially
> (from -> delete), but I just wasn't thinking
> clearly. I was cursing not having the ability to use a procmail
> filter while the solution was right in front of me.


> Regards, Stephen.


There, there, friend; we've all reached for the Big, Fancy Hammer
(BFH) when there was a lazier way to do it.
Best,
Chris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
String pattern matching Jim Lewis Python 9 04-16-2006 11:53 AM
Pattern matching with string and list olaufr@gmail.com Python 5 12-13-2005 10:40 PM
Pattern Matching Given # of Characters and no String Input; use RegularExpressions? Synonymous Python 10 04-22-2005 07:56 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57