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
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 ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
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.
With all these examples, now I see how /x works.
Cheers,
Parag
On Mon, Dec 27, 2010 at 2:39 PM, Tim Mitchell <tmit...@gmail.com> wrote:
> Try my $str = "HelloWorld";
>> --
>> To unsubscribe, e-mail: beginners-...@perl.org
>> For additional commands, e-mail: beginne...@perl.org
>> http://learn.perl.org/
>>
>>
>
>
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?
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?
> 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
> 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 =~/$str3/x){
if($str =~/ $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 =~ /hello\s*world/) {
or, you can first remove all the white space from your string before
attempting a match like:
(my $tmp=$str) =~ s/\s*//g; # assign $str3 to $tmp and then use s/
\s*//g to remove all white space from $tmp
if ($tmp =~ /helloworld/) { # now compare
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
SHC> On 10-12-27 07:11 PM, Uri Guttman wrote:
>>>>>>> "SHC" == Shawn H Corey<shawn...@gmail.com> 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.