Question about "?" character in Perl Regular Expression

A

Ahmad

When running the following example:

$str="aaaaaa bbbb";
($a,$b)= $str=~/(\w+)\s?(\w+)/;
print $a,"\n",$b;

The result is:

aaaaa
a

Why do we get this result??? Can anybody explain it?
thanks and regards,
Ahmad
 
P

Paul Lalli

When running the following example:

$str="aaaaaa   bbbb";
($a,$b)= $str=~/(\w+)\s?(\w+)/;
print $a,"\n",$b;

The result is:

aaaaa
a

Why do we get this result??? Can anybody explain it?

Christian already explained to you the difference between ? and +, but
perhaps you were also (or rather) confused about what \s actually is.
You may be thinking that \s means simply "whitespace". It actually
means "any single white-space character". It is any ONE character
from the group: space, tab, newline, carriage return, or vertical
tab. If you want to match more than one character in sequence, you
must use the quantifiers (+, or *, or {n}, depending on your needs)

Hope that helps,
Paul Lalli
 
J

Jürgen Exner

Ahmad said:
When running the following example:

$str="aaaaaa bbbb";
($a,$b)= $str=~/(\w+)\s?(\w+)/;
print $a,"\n",$b;

The result is:

aaaaa
a

Why do we get this result???

What did you expect instead?

jue
 
J

John W. Krahn

Paul said:
Christian already explained to you the difference between ? and +, but
perhaps you were also (or rather) confused about what \s actually is.
You may be thinking that \s means simply "whitespace". It actually
means "any single white-space character". It is any ONE character
from the group: space, tab, newline, carriage return, or vertical
tab.

Perl doesn't use the vertical tab. That should be the group: space,
horizontal tab, new line, carriage return or form feed.


John
 
A

Alan_C

Ahmad said:
When running the following example:

$str="aaaaaa bbbb";
($a,$b)= $str=~/(\w+)\s?(\w+)/;
print $a,"\n",$b;

The result is:

aaaaa
a

Why do we get this result??? Can anybody explain it?

(how you've used it) Like they've replied the ? simply makes the \s optional
as in it will match with or without \s in $str

(not how you've used it) Yet another or one more use (.+? and .*? only) (I
think) is to negate greed

$str="aaaaaa -bbbb -ccc";
($a,$b)= $str=~/(\w+).+?-(\w+)/; # negate greed
print $a,"\n",$b;
($a,$b)= $str=~/(\w+).+-(\w+)/;
print "\n\n", $a,"\n",$b, "\n";
# end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top