[perl-python] string pattern matching

Discussion in 'Python' started by Xah Lee, Feb 1, 2005.

  1. Xah Lee

    Xah Lee Guest

    # -*- 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
    Xah Lee, Feb 1, 2005
    #1
    1. Advertising

  2. 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
    Daniel Fackrell, Feb 1, 2005
    #2
    1. Advertising

  3. Xah Lee

    Dan Perl Guest

    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:...
    > 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
    >
    >
    >
    Dan Perl, Feb 1, 2005
    #3
  4. 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
    Erik Max Francis, Feb 2, 2005
    #4
  5. 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 mailing list to drop his
    posts on the floor so I don't have to read them.

    But thats me.

    Stephen.
    Stephen Thorne, Feb 2, 2005
    #5
  6. Xah Lee

    Chris Smith Guest

    >>>>> 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 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
    Chris Smith, Feb 2, 2005
    #6
  7. 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.
    Stephen Thorne, Feb 2, 2005
    #7
  8. - Henry Wadsworth Longfellow
    "Erik Max Francis" <> wrote in message
    news:...
    > 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
    Daniel Fackrell, Feb 2, 2005
    #8
  9. 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
    Erik Max Francis, Feb 2, 2005
    #9
  10. Xah Lee

    Chris Smith Guest

    >>>>> 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
    Chris Smith, Feb 2, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    4
    Views:
    611
    Aaron Sherman
    Feb 11, 2005
  2. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    233
    Marc Bissonnette
    Jan 13, 2004
  3. Xah Lee

    [perl-python] string pattern matching

    Xah Lee, Feb 1, 2005, in forum: Perl Misc
    Replies:
    0
    Views:
    131
    Xah Lee
    Feb 1, 2005
  4. Xah Lee
    Replies:
    1
    Views:
    193
    John Bokma
    Feb 7, 2005
  5. Bobby Chamness
    Replies:
    2
    Views:
    226
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page