[perl-python] string pattern matching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    [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

    Xah
    xah@xahlee.org


  • Daniel Fackrell

    #2
    Re: [perl-python] string pattern matching

    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



    Comment

    • Dan Perl

      #3
      Re: [perl-python] string pattern matching

      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" <unlearned@gmai l.com> wrote in message
      news:mailman.17 16.1107300060.2 2381.python-list@python.org ...[color=blue]
      > 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
      >
      >
      >[/color]


      Comment

      • Erik Max Francis

        #4
        Re: [perl-python] string pattern matching

        Dan Perl wrote:
        [color=blue]
        > 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.
        > ;-)[/color]

        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 && max@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

        Comment

        • Stephen Thorne

          #5
          Re: [perl-python] string pattern matching

          On Tue, 1 Feb 2005 18:59:18 -0500, Dan Perl <danperl@rogers .com> wrote:[color=blue]
          > 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.[/color]

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

          But thats me.

          Stephen.

          Comment

          • Chris Smith

            #6
            Re: [perl-python] string pattern matching

            >>>>> Stephen Thorne <stephen.thorne @gmail.com> writes:
            [color=blue]
            > On Tue, 1 Feb 2005 18:59:18 -0500, Dan Perl <danperl@rogers .com> wrote:[color=green]
            >> 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.[/color][/color]
            [color=blue]
            > I'd just like the python-list@python.org mailing list to drop
            > his posts on the floor so I don't have to read them.[/color]
            [color=blue]
            > But thats me.[/color]
            [color=blue]
            > Stephen.[/color]

            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

            Comment

            • Stephen Thorne

              #7
              Re: [perl-python] string pattern matching

              On Tue, 01 Feb 2005 21:19:34 -0500, Chris Smith
              <smitty_one_eac h@bigfoot.com> wrote:[color=blue]
              > 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.[/color]

              *headdesk*

              I'm using gmail, and I can set up the filter trivially
              (from:xah@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.

              Comment

              • Daniel Fackrell

                #8
                Re: [perl-python] string pattern matching

                - Henry Wadsworth Longfellow
                "Erik Max Francis" <max@alcyone.co m> wrote in message
                news:fbydnQJ8DN SthJ3fRVn-pw@speakeasy.ne t...[color=blue]
                > Dan Perl wrote:
                >[color=green]
                > > Perhaps someone will write a program to automatically follow up on every
                > > [perl-python] posting? The follow-up could just contain a statement[/color][/color]
                like[color=blue][color=green]
                > > the one Daniel mentions. Obviously the program would be written in[/color][/color]
                python.[color=blue][color=green]
                > > ;-)[/color]
                >
                > 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.[/color]

                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



                Comment

                • Erik Max Francis

                  #9
                  Re: [perl-python] string pattern matching

                  Daniel Fackrell wrote:
                  [color=blue]
                  > 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.[/color]

                  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 && max@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

                  Comment

                  • Chris Smith

                    #10
                    Re: [perl-python] string pattern matching

                    >>>>> Stephen Thorne <stephen.thorne @gmail.com> writes:
                    [color=blue]
                    > On Tue, 01 Feb 2005 21:19:34 -0500, Chris Smith
                    > <smitty_one_eac h@bigfoot.com> wrote:[color=green]
                    >> 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.[/color][/color]
                    [color=blue]
                    > *headdesk*[/color]
                    [color=blue]
                    > I'm using gmail, and I can set up the filter trivially
                    > (from:xah@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.[/color]
                    [color=blue]
                    > Regards, Stephen.[/color]

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

                    Comment

                    Working...