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

Regex - repeating a pattern within two anchors?

Expand Messages
  • hooyar66
    I have been struggling with a Regex and would appreciate some help. The regex needs to convert to underscores all the whitespace that is ... $line was
    Message 1 of 3 , Apr 1, 2007
    • 0 Attachment
      I have been struggling with a Regex and would appreciate some help.

      The regex needs to convert to underscores all the whitespace that is
      found between two quotation marks, so my test code should produce:

      ---
      $line was LED_5P05_2X10_2Y60_SMT "LED_SMT-YELLOW DIFFUSED
      REPLACE,1.90MMA" D1
      $line now LED_5P05_2X10_2Y60_SMT "LED_SMT-
      YELLOW_DIFFUSED_REPLACE,1.90MMA" D1
      ---

      I have made many unsuccessful attempts at a regex but am having a
      problem coding the correct pattern between the quote characters. I
      include what I believe is the closest to the correct code but produces

      ---
      $line was LED_5P05_2X10_2Y60_SMT "LED_SMT-YELLOW DIFFUSED
      REPLACE,1.90MMA" D1
      $line now LED_5P05_2X10_2Y60_SMT "D REPLACE,1.90MMA_D" D1
      ---

      Thanks for all and any help
      NJH


      #!c:/perl/bin/perl.exe -w

      use strict;
      use diagnostics;

      my $line = 'LED_5P05_2X10_2Y60_SMT "LED_SMT-YELLOW DIFFUSED
      REPLACE,1.90MMA" D1';

      print "\$line was $line\n";

      $line =~ s/\"((\S+)\s+(\S+))+\"/\"$1_$2\"/g;

      print "\$line now $line\n";

      #----------------
      #END
    • hooyar66
      ... is ... produces ... The code below is a solution - but is there a neater way? #!c:/perl/bin/perl.exe -w use strict; use diagnostics; my $line =
      Message 2 of 3 , Apr 1, 2007
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@...> wrote:
        >
        > I have been struggling with a Regex and would appreciate some help.
        >
        > The regex needs to convert to underscores all the whitespace that
        is
        > found between two quotation marks, so my test code should produce:
        >
        > ---
        > $line was LED_5P05_2X10_2Y60_SMT "LED_SMT-YELLOW DIFFUSED
        > REPLACE,1.90MMA" D1
        > $line now LED_5P05_2X10_2Y60_SMT "LED_SMT-
        > YELLOW_DIFFUSED_REPLACE,1.90MMA" D1
        > ---
        >
        > I have made many unsuccessful attempts at a regex but am having a
        > problem coding the correct pattern between the quote characters. I
        > include what I believe is the closest to the correct code but
        produces
        >

        > ---
        > $line was LED_5P05_2X10_2Y60_SMT "LED_SMT-YELLOW DIFFUSED
        > REPLACE,1.90MMA" D1
        > $line now LED_5P05_2X10_2Y60_SMT "D REPLACE,1.90MMA_D" D1
        > ---
        >
        > Thanks for all and any help
        > NJH
        >
        >
        > #!c:/perl/bin/perl.exe -w
        >
        > use strict;
        > use diagnostics;
        >
        > my $line = 'LED_5P05_2X10_2Y60_SMT "LED_SMT-YELLOW DIFFUSED
        > REPLACE,1.90MMA" D1';
        >
        > print "\$line was $line\n";
        >
        > $line =~ s/\"((\S+)\s+(\S+))+\"/\"$1_$2\"/g;
        >
        > print "\$line now $line\n";
        >
        > #----------------
        > #END
        >


        The code below is a solution - but is there a neater way?

        #!c:/perl/bin/perl.exe -w

        use strict;
        use diagnostics;

        my $line = 'LED_5P05_2X10_2Y60_SMT "LED_SMT-YELLOW DIFFUSED
        REPLACE,1.90MMA" D1';

        print "\$line was: $line";

        $line =~ /\"(.+)\"/;
        my $str = $1;
        my $str2 = $str;
        $str2 =~ s/\s/_/g;

        $line =~ s/$str/$str2/;

        print "\$line now: $line\n";
      • merlyn@stonehenge.com
        ... This is dangerous: hooyar66 $line =~ / (.+) /; because if it *doesn t* match, then: hooyar66 my $str = $1; the $1 there is the *previous* $1. Bad.
        Message 3 of 3 , Apr 1, 2007
        • 0 Attachment
          >>>>> "hooyar66" == hooyar66 <pcbcad@...> writes:

          This is dangerous:

          hooyar66> $line =~ /\"(.+)\"/;

          because if it *doesn't* match, then:

          hooyar66> my $str = $1;

          the $1 there is the *previous* $1. Bad. Very bad idea.

          NEVER use $1 except in a context where you have validated that the
          match has actually succeeded.

          --
          Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
          <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
          Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
          See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
        Your message has been successfully submitted and would be delivered to recipients shortly.