regular expression

regular expression

am 28.04.2011 16:05:12 von Irfan Sayed

--0-864565372-1303999512=:5396
Content-Type: text/plain; charset=us-ascii

hi,

i have following code.


$target = "abc,xyz";
print "$target\n";
$target =~ s/,/\s/g;
print "$target\n";

i need to replace "comma" with whitespace for string "abc,xyz"

the output shud be "abc xyz"


the above regular expression does not do that . please suggest


--irfan
--0-864565372-1303999512=:5396--

Re: regular expression

am 28.04.2011 16:17:53 von jwkrahn

Irfan Sayed wrote:
> hi,

Hello,


> i have following code.
>
>
> $target = "abc,xyz";
> print "$target\n";
> $target =~ s/,/\s/g;
> print "$target\n";
>
> i need to replace "comma" with whitespace for string "abc,xyz"

"Whitespace" is something that applies only to regular expressions but
the second part of the substitution operator is just a string, not a
regular expression. And which of the five whitespace characters should
this string interpolate "\s" as: " ", "\r", "n", "t" or "\f"?


> the output shud be "abc xyz"
>
>
> the above regular expression does not do that . please suggest

$target =~ tr/,/ /;



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: regular expression

am 28.04.2011 16:20:01 von jwkrahn

John W. Krahn wrote:
> Irfan Sayed wrote:
>
>> i have following code.
>>
>>
>> $target = "abc,xyz";
>> print "$target\n";
>> $target =~ s/,/\s/g;
>> print "$target\n";
>>
>> i need to replace "comma" with whitespace for string "abc,xyz"
>
> "Whitespace" is something that applies only to regular expressions but
> the second part of the substitution operator is just a string, not a
> regular expression. And which of the five whitespace characters should
> this string interpolate "\s" as: " ", "\r", "n", "t" or "\f"?

Should be: " ", "\r", "\n", "\t" or "\f"



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: regular expression

am 28.04.2011 16:25:33 von Shawn H Corey

On 11-04-28 10:05 AM, Irfan Sayed wrote:
> hi,
>
> i have following code.
>
>
> $target = "abc,xyz";
> print "$target\n";
> $target =~ s/,/\s/g;
> print "$target\n";
>
> i need to replace "comma" with whitespace for string "abc,xyz"
>
> the output shud be "abc xyz"
>
>
> the above regular expression does not do that . please suggest
>
>
> --irfan

The white space meta-character does not work on the replace side of a
substitution. Use a space character:

$target =~ s/\,/ /g;

Some people prefer to use the hexadecimal format because in some fonts,
it's hard to distinguish between // and / /. Having something visible
makes the meaning clear.

Like this:

$target =~ s/\,/\x20/g;


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: regular expression

am 28.04.2011 16:28:54 von Irfan Sayed

--0-1933330801-1304000934=:50433
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

my logic was to just put the space character in place of comma and keep res=
t as it is but unfortunately that does not work thanks john for=
your trick =0Ait is working now =0A____________________________=
____=0AFrom: John W. Krahn =0ATo: Perl Beginners s@perl.org>=0ASent: Thursday, April 28, 2011 7:47 PM=0ASubject: Re: regular=
expression Irfan Sayed wrote:=0A> hi, Hello, =0A> i have fo=
llowing code.=0A>=0A>=0A> $target =3D "abc,xyz";=0A> print "$target\n";=0A>=
$target =3D~ s/,/\s/g;=0A> print "$target\n";=0A>=0A> i need to replace "c=
omma" with whitespace for string "abc,xyz" "Whitespace" is something t=
hat applies only to regular expressions but =0Athe second part of the subst=
itution operator is just a string, not a =0Aregular expression.=A0 And whic=
h of the five whitespace characters should =0Athis string interpolate "\s" =
as: " ", "\r", "n", "t" or "\f"? =0A> the output shud be "abc xyz"=0A>=
=0A>=0A> the above regular expression does not do that . please suggest=0A=
=0A$target =3D~ tr/,/ /; John=0A-- =0AAny intelligent fool can m=
ake things bigger and=0Amore complex... It takes a touch of genius -=0Aand =
a lot of courage to move in the opposite=0Adirection.=A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 -- Albert Einstein -- =0ATo unsubscribe, e-mail: begi=
nners-unsubscribe@perl.org=0AFor additional commands, e-mail: beginners-hel=
p@perl.org=0Ahttp://learn.perl.org/
--0-1933330801-1304000934=:50433--

Re: regular expression

am 28.04.2011 16:30:04 von Irfan Sayed

--0-58235633-1304001004=:25994
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

thanks all ________________________________=0AFrom: Shawn H Core=
y =0ATo: beginners@perl.org=0ASent: Thursday, April 28,=
2011 7:55 PM=0ASubject: Re: regular expression On 11-04-28 10:05 AM, =
Irfan Sayed wrote:=0A> hi,=0A>=0A> i have following code.=0A>=0A>=0A> $targ=
et =3D "abc,xyz";=0A> print "$target\n";=0A> $target =3D~ s/,/\s/g;=0A> pri=
nt "$target\n";=0A>=0A> i need to replace "comma" with whitespace for strin=
g "abc,xyz"=0A>=0A> the output shud be "abc xyz"=0A>=0A>=0A> the above regu=
lar expression does not do that . please suggest=0A>=0A>=0A> --irfan T=
he white space meta-character does not work on the replace side of a =0Asub=
stitution.=A0 Use a space character: $target =3D~ s/\,/ /g; Some =
people prefer to use the hexadecimal format because in some fonts, =0Ait's =
hard to distinguish between // and / /.=A0 Having something visible =0Amake=
s the meaning clear. Like this: $target =3D~ s/\,/\x20/g; =
=0A-- =0AJust my 0.00000002 million dollars worth,   Shawn Confus=
ion is the first step of understanding. Programming is as much about o=
rganization and communication=0Aas it is about coding. The secret to g=
reat software:=A0 Fail early & often. Eliminate software piracy:=A0 us=
e only FLOSS. -- =0ATo unsubscribe, e-mail: beginners-unsubscribe@perl=
..org=0AFor additional commands, e-mail: beginners-help@perl.org=0Ahttp://le=
arn.perl.org/
--0-58235633-1304001004=:25994--

Re: regular expression

am 28.04.2011 17:03:29 von Karl Kaufman

----- Original Message -----
From: "Irfan Sayed"
To: "John W. Krahn" ; "Perl Beginners"
Sent: Thursday, April 28, 2011 9:28 AM
Subject: Re: regular expression
>
> my logic was to just put the space character in place of comma and keep
> rest as it is
>
> but unfortunately that does not work

Well, to be precise, your conceptual logic was fine; the implementation was
flawed. As several have pointed out, you weren't replacing the comma with a
_space_ *character*, but with the RegExp _whitespace_ *character class*.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: regular expression

am 28.04.2011 18:17:42 von Uri Guttman

>>>>> "KK" == Karl Kaufman writes:

KK> Well, to be precise, your conceptual logic was fine; the
KK> implementation was flawed. As several have pointed out, you
KK> weren't replacing the comma with a _space_ *character*, but with
KK> the RegExp _whitespace_ *character class*.

to be really precise, there was no character class in that code.
he was replacing the comma with the letter s. the right side of s///
is normally just a double quoted string and \s becomes just an s. this
shows it:

perl -le '$x = "a,b" ; $x =~ s/,/\s/; print $x'
asb



--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/