Question about "?" character in Perl Regular Expression
Question about "?" character in Perl Regular Expression
am 02.01.2008 08:58:14 von 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
Re: Question about "?" character in Perl Regular Expression
am 02.01.2008 09:43:01 von Christian Winter
Ahmad wrote:
> 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?
The regex you're using says:
- match (greedily) any number of word characters
- followed by one or zero whitespaces
- followed by any number of word characters
The only possible place where "one or zero whitespaces
followed by a word character" can match directly after a
word character is inside the "aaaaaa" substring.
You most probably want to use "+" instead of "?" as
the whitespace quantifier (match one or more occurences).
($a,$b)= $str=~/(\w+)\s+(\w+)/;
-Chris
Re: Question about "?" character in Perl Regular Expression
am 02.01.2008 18:52:58 von Jim Gibson
In article <477b4e95$0$16666$9b4e6d93@newsspool3.arcor-online.net>,
Christian Winter wrote:
> Ahmad wrote:
> > 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?
>
> The regex you're using says:
> - match (greedily) any number of word characters
> - followed by one or zero whitespaces
> - followed by any number of word characters
>
> The only possible place where "one or zero whitespaces
> followed by a word character" can match directly after a
> word character is inside the "aaaaaa" substring.
Well, to be precise, it could also match inside the "bbbb" substring at
any of three positions, but it doesn't get that far because of the
earlier match.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: Question about "?" character in Perl Regular Expression
am 02.01.2008 20:54:37 von Paul Lalli
On Jan 2, 2:58=A0am, Ahmad wrote:
> When running the following example:
>
> $str=3D"aaaaaa =A0 bbbb";
> ($a,$b)=3D $str=3D~/(\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
Re: Question about "?" character in Perl Regular Expression
am 02.01.2008 21:07:15 von jurgenex
Ahmad wrote:
>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
Re: Question about "?" character in Perl Regular Expression
am 02.01.2008 21:13:12 von someone
Paul Lalli wrote:
> On Jan 2, 2:58 am, Ahmad wrote:
>> 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.
Perl doesn't use the vertical tab. That should be the group: space,
horizontal tab, new line, carriage return or form feed.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Re: Question about "?" character in Perl Regular Expression
am 03.01.2008 08:50:31 von Alan_C
Ahmad wrote:
> 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
--
AC