By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,062 Members | 1,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,062 IT Pros & Developers. It's quick & easy.

[perl-python] string pattern matching

P: n/a
# -*- 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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
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" <un*******@gmail.com> wrote in message
news:ma***************************************@pyt hon.org...
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

Jul 18 '05 #3

P: n/a
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 && ma*@alcyone.com && 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
Jul 18 '05 #4

P: n/a
On Tue, 1 Feb 2005 18:59:18 -0500, Dan Perl <da*****@rogers.com> 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 py*********@python.org mailing list to drop his
posts on the floor so I don't have to read them.

But thats me.

Stephen.
Jul 18 '05 #5

P: n/a
>>>>> Stephen Thorne <st************@gmail.com> writes:
On Tue, 1 Feb 2005 18:59:18 -0500, Dan Perl <da*****@rogers.com> 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 py*********@python.org 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
Jul 18 '05 #6

P: n/a
On Tue, 01 Feb 2005 21:19:34 -0500, Chris Smith
<sm*************@bigfoot.com> 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:xa*@xahlee.org -> 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.
Jul 18 '05 #7

P: n/a
- Henry Wadsworth Longfellow
"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:fb********************@speakeasy.net...
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

Jul 18 '05 #8

P: n/a
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 && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Divorces are made in Heaven.
-- Oscar Wilde
Jul 18 '05 #9

P: n/a
>>>>> Stephen Thorne <st************@gmail.com> writes:
On Tue, 01 Feb 2005 21:19:34 -0500, Chris Smith
<sm*************@bigfoot.com> 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:xa*@xahlee.org -> 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
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.