Loading ...
Sorry, an error occurred while loading the content.

Re: [Clip] Use RegExp to change case?

Expand Messages
  • lonewolf_tamu
    Hi all I am attempting to implement Perl regexp expressions to take advantage of non-greedy matching. Unfortunately, I am not very familiar with Perl. I have
    Message 1 of 5 , May 6, 2005
    • 0 Attachment
      Hi all

      I am attempting to implement Perl regexp expressions to take
      advantage of "non-greedy" matching. Unfortunately, I am not very
      familiar with Perl.

      I have had partial success by modifying the script below, posted more
      than 2 years ago. This script seems to operate on individual blocks
      of text seperated by blank lines. I need it to treat the entire
      document as one block of text, so blank lines can be contained within
      the matched text.

      I tried removing "while (<>)" from the script, which resulted in my
      entire document being erased. I had the same problem when I tried
      writing a script from scratch, which is why I searched for and
      modified a working script from the forum.

      Can some Perl guru help me?

      Daniel T. Calmes

      Source: http://groups.yahoo.com/group/ntb-clips/message/9979
      --- In ntb-clips@yahoogroups.com, "Sheri" <silvermoonwoman@c...>
      wrote:
      > Hi Alan,
      >
      > Cool stuff, your Perl case changer! Your patterns also work fine in
      > my Perl Regexp dialog. I had read about .*? before, but forgot
      about
      > it.
      >
      > One enhancement would be to treat the newlines as dot characters by
      > using an "s" modifier together with either the paragraph or whole
      > file unit. That way the opening tag, text and closing tag don't
      have
      > to be on the same line for it to work. When I get a chance I think
      I
      > will add an option for the "s" modifier to my dialog. Below is your
      > perl script modified to operate on sections separated by blank
      lines
      > (what I call paragraphs).
      >
      > Regards,
      > Sheri
      >
      > H="_pl_case_changer"
      > #!perl
      > local $/="";
      > # Works entire doc if nothing is selected
      > # Or, works just your selection if text is selected
      > while (<>) { # ea section between blank lines
      > s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis;
      > print ;
      > } # end
    • Alan C.
      Hi Daniel, I m certainly no guru. But am recently leave the basics behind for intermediate and advanced Perl programming things such as: packages, namespaces,
      Message 2 of 5 , May 7, 2005
      • 0 Attachment
        Hi Daniel,

        I'm certainly no guru. But am recently leave the basics behind for intermediate and advanced Perl programming things such as: packages, namespaces, references, complex data structures, and perhaps now not too far away on the horizon: Perl Object Oriented programming.

        The next worked here (see enclosed sample). Does it work for you?

        H="pl_case_changr"
        ^!RunPerl pl_case_changer

        H="_pl_case_changer
        #!perl -w
        undef $/; # Enter "file slurp" mode.
        $text = <>; # This file/buffer/selection slurped into the scalar
        for ($text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis) {
        print $text;
        } # end

        -------------begin sample text---------------------

        the rain <strong> FALLS AND RUNS DOWN THE DRIVEWAY </strong> today

        the rain <strong> FALLS AND

        RUNS DOWN THE DRIVEWAY </strong> today
        ---------

        Formerly the sample text was all lower case. Did not select anything. cursor at top of doc. launched: pl_case_changr

        This slurps an entire file into the $text variable. It's recommended not to do this on a real large file due to the memory consumption required.

        foreach might also work in place of the for

        I was trying to say: for each occurrence of my sub string, uppercase each

        A perl guru rather likely could improve upon.

        Alan.

        On Fri, 06 May 2005 19:44:59 -0000, lonewolf_tamu wrote:
        > Hi all
        >
        > I am attempting to implement Perl regexp expressions to take
        > advantage of "non-greedy" matching. Unfortunately, I am not very
        > familiar with Perl.
        >
        > I have had partial success by modifying the script below, posted more
        > than 2 years ago. This script seems to operate on individual blocks
        > of text seperated by blank lines. I need it to treat the entire
        > document as one block of text, so blank lines can be contained within
        > the matched text.
        >
        > I tried removing "while (<>)" from the script, which resulted in my
        > entire document being erased. I had the same problem when I tried
        > writing a script from scratch, which is why I searched for and
        > modified a working script from the forum.
        >
        > Can some Perl guru help me?
        >
        > Daniel T. Calmes
        >
        > Source: http://groups.yahoo.com/group/ntb-clips/message/9979
        > --- In ntb-clips@yahoogroups.com, "Sheri" <silvermoonwoman@c...>
        > wrote:
        >> Hi Alan,
        >>
        >> Cool stuff, your Perl case changer! Your patterns also work fine in
        >> my Perl Regexp dialog. I had read about .*? before, but forgot
        > about
        >> it.
        >>
        >> One enhancement would be to treat the newlines as dot characters by
        >> using an "s" modifier together with either the paragraph or whole
        >> file unit. That way the opening tag, text and closing tag don't
        > have
        >> to be on the same line for it to work. When I get a chance I think
        > I
        >> will add an option for the "s" modifier to my dialog. Below is your
        >> perl script modified to operate on sections separated by blank
        > lines
        >> (what I call paragraphs).
        >>
        >> Regards,
        >> Sheri
        >>
        >> H="_pl_case_changer"
        >> #!perl
        >> local $/="";
        >> # Works entire doc if nothing is selected
        >> # Or, works just your selection if text is selected
        >> while (<>) { # ea section between blank lines
        >> s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis;
        >> print ;
        >> } # end
      • Sheri
        ... LOL! I ve forgotten everything I m so out of practice I ve forgotten everything I knew.
        Message 3 of 5 , May 11, 2005
        • 0 Attachment
          At 06:07 PM 5/7/2005 -0700, you wrote:
          >Hi Daniel,
          >
          >I'm certainly no guru. But am recently leave the basics behind for intermediate and advanced Perl programming things such as: packages, namespaces, references, complex data structures, and perhaps now not too far away on the horizon: Perl Object Oriented programming.

          LOL! I've forgotten everything I'm so out of practice I've forgotten everything I knew.
        • acumming@cwnet.com
          ... and advanced Perl programming things such as: packages, namespaces, references, complex data structures, and perhaps now not too far away on the horizon:
          Message 4 of 5 , May 11, 2005
          • 0 Attachment
            On Wed, 11 May 2005 10:40 , Sheri <silvermoonwoman@...> sent:

            >At 06:07 PM 5/7/2005 -0700, you wrote:
            >>Hi Daniel,
            >>
            >>I'm certainly no guru. But am recently leave the basics behind for intermediate
            and advanced Perl programming things such as: packages, namespaces, references,
            complex data structures, and perhaps now not too far away on the horizon: Perl
            Object Oriented programming.
            >
            >LOL! I've forgotten everything I'm so out of practice I've forgotten everything I
            knew.

            Hi! Well it's good to hear your voice on the list once again!

            I'm a couple years behind schedule. But I must be facsinated (sp) with the
            capabilities of Perl: enough familiarity with it then often equates to like "abbra
            cadabbra" sorts of things/capabilities.

            (See next too). But probably what it was doing with the for loop in it is that it
            was just doing one loop then finishing, ie processes all replacements in the first
            loop resulting in it's completed which in turn results in exiting the for loop so
            that therefore the for loop was/is not even needed. Ok, the gurus didn't have time
            to look or else perhaps they would have pointed that out to me.

            The good news is that I'm beginning to catch my own.

            (I'll try to remember to test this when I get on my other machine) A guru would
            know -- but likely could do without the for loop and I think it is so because of
            the g (global option) coupled with the regex operator already does goes through the
            entire string, doing all replacements. For example, to remove the for loop from
            the script:

            for ($text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis) {
            print $text;
            } # end

            So, those 3 lines would then instead become (and more correctly I think too) 2
            lines, thus:

            $text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis);
            print $text; # end

            (The unmodified original is enclosed next):

            H="pl_case_changr"
            ^!RunPerl pl_case_changer

            H="_pl_case_changer"
            #!perl -w
            undef $/; # Enter "file slurp" mode.
            $text = <>; # This file/buffer/selection slurped into the scalar
            for ($text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis) {
            print $text;
            } # end

            Alan.




            ---- Msg sent via CWNet - http://www.cwnet.com/
          • acummingsus
            ... [ . . ] A guru would ... so because of ... goes through the ... for loop from ... think too) 2 ... Oops. I removed the left hand parenthesis but forgot to
            Message 5 of 5 , May 11, 2005
            • 0 Attachment
              --- In ntb-scripts@yahoogroups.com, <acumming@c...> wrote:
              [ . . ] A guru would
              > know -- but likely could do without the for loop and I think it is
              so because of
              > the g (global option) coupled with the regex operator already does
              goes through the
              > entire string, doing all replacements. For example, to remove the
              for loop from
              > the script:
              >
              > for ($text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis) {
              > print $text;
              > } # end
              >
              > So, those 3 lines would then instead become (and more correctly I
              think too) 2
              > lines, thus:
              >
              > $text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis);
              > print $text; # end

              Oops. I removed the left hand parenthesis but forgot to remove the
              one on the right hand side.

              gis) needs to become gis

              $text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis;

              That's the only change, one ) removed. The above line is corrected.
              Alan.
              >
              > (The unmodified original is enclosed next):
              >
              > H="pl_case_changr"
              > ^!RunPerl pl_case_changer
              >
              > H="_pl_case_changer"
              > #!perl -w
              > undef $/; # Enter "file slurp" mode.
              > $text = <>; # This file/buffer/selection slurped into the scalar
              > for ($text =~ s/(<strong>)(.*?)(<\/strong>)/$1\U$2\E$3/gis) {
              > print $text;
              > } # end
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.