On Dec 27, 2:34 pm, paragka...@gmail.com (Parag Kalra) wrote:
> Hi,
>
> I was under the impression that regex modifier '/x' ignores the white
> space. So in following script both the if-else blocks should print
> "Match" since the strings differ only in white space and '/x' should
> ignore the white space.
>
> But looks like my understanding of '/x' is wrong. Could someone please
> correct me and explain the below behavior.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> my $str = "Hello World";
> my $str2 = "Hello World";
> my $str3 = "Hello    World";
>
> if($str =~/$str2/x){
>     print "Match\n";} else {
>
>     print "No Match\n";
>
> }
>
> if($str =~/$str3/x){
>     print "Match\n";} else {
>
>     print "No Match\n";
>
> }

There's a twist that may be confusing though.  Double
quote interpolation occurs first before further regex
parsing proceeds.

So, for instance,  /$str2/x  gets interpolated and  the
regex parser now sees  'Hello World'. After stripping
whitespace because of /x, that becomes  'HelloWorld'.

Therefore 'Hello World' doesn't match 'HelloWorld' and
'No Match' is the result.

--
Charles DeRykus


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


Reply via email to