473,725 Members | 2,271 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 3214
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
3455
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
4687
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
3637
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
3261
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
3011
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
34427
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
8752
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,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
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...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
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
4519
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...
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.