Behavior of Regex modifier /x

Behavior of Regex modifier /x

am 27.12.2010 23:34:06 von Parag Kalra

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";
}


Cheers,
Parag

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

Re: Behavior of Regex modifier /x

am 28.12.2010 00:19:01 von mark

On Dec 27, 2:34=A0pm, paragka...@gmail.com (Parag Kalra) wrote:

> I was under the impression that regex modifier '/x' ignores the white
> space.

It ignores white space in the regular expression, not in the text you
are matching.

For example, the following are equivalent:

if($str =3D~/$str3/x){

if($str =3D~/ $str3 /x){

To do what you want, you can use '\s*' in your regular expression
where ever white space that you want to ignore can be:

if ($str =3D~ /hello\s*world/) {

or, you can first remove all the white space from your string before
attempting a match like:

(my $tmp=3D$str) =3D~ s/\s*//g; # assign $str3 to $tmp and then use s/
\s*//g to remove all white space from $tmp
if ($tmp =3D~ /helloworld/) { # now compare


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

Re: Behavior of Regex modifier /x

am 28.12.2010 00:30:24 von Uri Guttman

>>>>> "PK" == Parag Kalra writes:

PK> I was under the impression that regex modifier '/x' ignores the
PK> white space. So in following script both the if-else blocks should
PK> print "Match" since the strings differ only in white space and
PK> '/x' should ignore the white space.

/x only ignores white space in the regex itself, not in the data. /x is
meant to allow regexes to be spread out and over multiple lines by using
(ignored) white space for formatting. /x also allows comments which are
very helpful in complex regexes.

PK> But looks like my understanding of '/x' is wrong. Could someone please
PK> correct me and explain the below behavior.

yes, you have misunderstood it. where did you read or learn that /x
ignores whitespace in the data?

uri

--
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/

Re: Behavior of Regex modifier /x

am 28.12.2010 00:46:22 von Shawn H Corey

On 10-12-27 06:30 PM, Uri Guttman wrote:
> yes, you have misunderstood it. where did you read or learn that /x
> ignores whitespace in the data?

He probably got the idea since other non-alphanumeric characters in a
variable must be escaped; un-escaped ones act like meta-characters.

$ perl -e'$v="+";$ARGV[0]=~/1$v/&&print"yes\n"' 0
$ perl -e'$v="+";$ARGV[0]=~/1$v/&&print"yes\n"' 1
yes
$


--
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: Behavior of Regex modifier /x

am 28.12.2010 00:46:42 von rvtol+usenet

On 2010-12-27 23:34, Parag Kalra wrote:

> 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.

See perlre:

x Extend your pattern’s legibility by permitting whitespace
and comments.

Confusing indeed when you come from (for example) sed, where the
'pattern' is the data.

So read 'regex' where it says 'pattern' there.

--
Ruud

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

Re: Behavior of Regex modifier /x

am 28.12.2010 01:10:52 von Parag Kalra

Thanks All.

With all these examples, now I see how /x works.

Cheers,
Parag




On Mon, Dec 27, 2010 at 2:39 PM, Tim Mitchell wrote:
> Try my $str =3D "HelloWorld";
>
> On Mon, Dec 27, 2010 at 2:34 PM, 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 =3D "Hello World";
>> my $str2 =3D "Hello World";
>> my $str3 =3D "Hello    World";
>>
>> if($str =3D~/$str2/x){
>>    print "Match\n";
>> } else {
>>    print "No Match\n";
>> }
>>
>> if($str =3D~/$str3/x){
>>    print "Match\n";
>> } else {
>>    print "No Match\n";
>> }
>>
>>
>> Cheers,
>> Parag
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>> For additional commands, e-mail: beginners-help@perl.org
>> http://learn.perl.org/
>>
>>
>
>

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

Re: Behavior of Regex modifier /x

am 28.12.2010 01:11:03 von Uri Guttman

>>>>> "SHC" == Shawn H Corey writes:

SHC> On 10-12-27 06:30 PM, Uri Guttman wrote:
>> yes, you have misunderstood it. where did you read or learn that /x
>> ignores whitespace in the data?

SHC> He probably got the idea since other non-alphanumeric characters in a
SHC> variable must be escaped; un-escaped ones act like meta-characters.

that doesn't make any sense regarding the use of /x.

from perlretut (which ALL regex newbies should read):


Long regexps like this may impress your friends, but can be hard
to decipher. In complex situations like this, the "//x"
modifier for a match is invaluable. It allows one to put nearly
arbitrary whitespace and comments into a regexp without
affecting their meaning. Using it, we can rewrite our
'extended' regexp in the more pleasing form

looks pretty clear to me that means whitespace in the regex
itself. nothing to do with escaping or meta chars. also there are
non-alphanumerics that aren't metachars in regexes.


perlre has this longer part on /x:

The "/x" modifier itself needs a little more explanation. It
tells the regular expression parser to ignore whitespace that is
neither backslashed nor within a character class. You can use
this to break up your regular expression into (slightly) more
readable parts. The "#" character is also treated as a
metacharacter introducing a comment, just as in ordinary Perl
code. This also means that if you want real whitespace or "#"
characters in the pattern (outside a character class, where they
are unaffected by "/x"), then you'll either have to escape them
(using backslashes or "\Q...\E") or encode them using octal or
hex escapes. Taken together, these features go a long way
towards making Perl's regular expressions more readable. Note
that you have to be careful not to include the pattern delimiter
in the comment--perl has no way of knowing you did not intend to
close the pattern early. See the C-comment deletion code in
perlop. Also note that anything inside a "\Q...\E" stays
unaffected by "/x".

that does mention escaping whitespace to make it signifigant under
/x. but it clearly states the regex parser and no where does it talk
about whitespace in the data being ignored.

so my question is still valid, where and how did the OP mistakenly learn
that /x ignores whitespace in the data being matched?

uri

--
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/

Re: Behavior of Regex modifier /x

am 28.12.2010 01:17:26 von Shawn H Corey

On 10-12-27 07:11 PM, Uri Guttman wrote:
>>>>>> "SHC" == Shawn H Corey writes:
> SHC> On 10-12-27 06:30 PM, Uri Guttman wrote:
> >> yes, you have misunderstood it. where did you read or learn that /x
> >> ignores whitespace in the data?
>
> SHC> He probably got the idea since other non-alphanumeric characters in a
> SHC> variable must be escaped; un-escaped ones act like meta-characters.
>
> that doesn't make any sense regarding the use of /x.

Yes, it does. Meta-characters in a variable act the same as in a
pattern. Whitespace in a variable does NOT act the same as in a
pattern. Why?


--
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: Behavior of Regex modifier /x

am 28.12.2010 02:08:10 von derykus

On Dec 27, 2:34=A0pm, 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 =3D "Hello World";
> my $str2 =3D "Hello World";
> my $str3 =3D "Hello =A0 =A0World";
>
> if($str =3D~/$str2/x){
> =A0 =A0 print "Match\n";} else {
>
> =A0 =A0 print "No Match\n";
>
> }
>
> if($str =3D~/$str3/x){
> =A0 =A0 print "Match\n";} else {
>
> =A0 =A0 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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Behavior of Regex modifier /x

am 28.12.2010 02:31:35 von Uri Guttman

>>>>> "SHC" == Shawn H Corey writes:

SHC> On 10-12-27 07:11 PM, Uri Guttman wrote:
>>>>>>> "SHC" == Shawn H Corey writes:
SHC> On 10-12-27 06:30 PM, Uri Guttman wrote:
>> >> yes, you have misunderstood it. where did you read or learn that /x
>> >> ignores whitespace in the data?
>>
SHC> He probably got the idea since other non-alphanumeric characters in a
SHC> variable must be escaped; un-escaped ones act like meta-characters.
>>
>> that doesn't make any sense regarding the use of /x.

SHC> Yes, it does. Meta-characters in a variable act the same as in a
SHC> pattern. Whitespace in a variable does NOT act the same as in a
SHC> pattern. Why?

because you enabled /x. still no reason for the OP to read the docs and
not understand /x. they even cover escaping whitespace so it WILL match.

on top of that, ignoring whitespace in the data is trivial. either first
remove it all with tr/// or use \s* where you think whitespace may
happen.

uri

--
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/