List Info

Thread: Regex - repeating a pattern within two anchors?




Regex - repeating a pattern within two anchors?
country flaguser name
United States
2007-04-01 07:04:16

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 $linen";

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

print "$line now $linen";

#----------------
#END

__._,_.___
.

__,_._,___
Re: Regex - repeating a pattern within two anchors?
country flaguser name
United States
1969-12-31 18:00:00

--- In perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com, "hooyar66" <pcbcad...> wrote:
&gt;
> 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&quot; 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 $linen&quot;;
>
> $line =~ s/"((S+)s+(S+))+"/&quot;$1_$2&quot;/g;
>
> print "$line now $linen&quot;;
>
> #----------------
> #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&quot; D1';

print "$line was: $line";;

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

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

print "$line now: $linen&quot;;

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )