473,703 Members | 3,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[perl-python] string pattern matching

# -*- 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(patte rn, text)
©
©if result:
© print 'yes'
© print result.expand(r "\1") # the captured pattern
©else:
© print 'no'

#-------------
# if re.search(patte rn, 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
9 3212
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
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*******@gmai l.com> wrote in message
news:ma******** *************** *************** *@python.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
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
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*********@pyt hon.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
>>>>> 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*********@pyt hon.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
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*@xahle e.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
- Henry Wadsworth Longfellow
"Erik Max Francis" <ma*@alcyone.co m> wrote in message
news:fb******** ************@sp eakeasy.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
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 institutionaliz ation 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
>>>>> 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*@xahle e.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8133
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: ------------------------------------- test3.pl ------------------------------------- print "PERL Hello from Perl! (plain print)<br>\n"; print STDERR "PERL This is text sent to STDERR<br>\n"; $output="PERL Some output:<br>\n"; for ($i=0; $i<5; $i++) {
6
5949
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question is this... is Perl so much better at parsing text files and outputing that we would see a substantial speed increase? We process about 10 million records in flat files a day for reformatting before putting them in a DB. Also, when it comes to...
3
3454
by: David F. Skoll | last post by:
Hi, I'm tearing my hair out on this one. I'm trying to embed a Perl interpreter into a C program. I need to be able to create and destroy the interpreter periodically, but will never actually have two interpreters at the same time. On Red Hat Linux 7.3 with Perl 5.6.1, the attached program segfaults. On Red Hat 9 with Perl 5.8.0, it works perfectly. Save the program code as "test-embed-perl.c" and the build script as "buildte". ...
1
4685
by: Julia Bell | last post by:
I would like to run the same script on two different platforms. The directory in which the script(s) will be stored is common to the two platforms. (I see the same directory contents regardless of which platform I use to access the directory.) Platform 1: perl is installed in /tps/bin/perl. CPAN modules are available Perl is also installed in /usr/bin/perl Platform 1, but the modules are not accessible with this version. Platform...
1
3633
by: smsabu2002 | last post by:
Hi, I am facing the build problem while installing the DBD-MySql perl module (ver 2.9008) using both GCC and CC compilers in HP-UX machine. For the Build using GCC, the compiler error is produced due to the unknown GCC compiler option "+DAportable". For the Build using CC, the preprocessor error is produced due to the recursive declration of macro "PerlIO" in perlio.h file.
13
3258
by: Otto J. Makela | last post by:
I'm trying to install to php the Perl-1.0.0.tgz package (from http://pecl.php.net/package/perl, enabling one to call perl libraries) to a pre-existing Solaris system. Unfortunately, the attempt fails in a rather dramatic way, spewing out thousands of "relocation remains"... I'm somewhat lost on what to do next, the documentation that came along with the Perl package is somewhat sparse. Anyone have suggestions? % uname -a
6
3009
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation for one of the Pear modules for creating XML and it didn't look like much. I've used Perl XML:Twig and I have the impression that there is more Perl stuff available as well as the Perl stuff being well documented as I have a Perl DBI book, a Perl...
4
3706
by: billb | last post by:
I installed a perl extension for PHP to use some perl inside my php primarily because I have perl working with oracle and not php and oracle. So I want to use my old perl scripts, and use the functionality of php. The extension uses the '$perl->eval' function. i am kind of stuck with the syntax or how to put the php variable into the perl script. I have a form where the user puts in a grid reference. Then a php script that gets the...
21
34417
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
0
8674
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6595
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5923
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4434
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2463
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.