Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question about "?" character in Perl Regular Expression

Reply
Thread Tools

Question about "?" character in Perl Regular Expression

 
 
Ahmad
Guest
Posts: n/a
 
      01-02-2008
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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-02-2008
On Jan 2, 2:58*am, Ahmad <ahmad.abdulgh...@gmail.com> 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. 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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-02-2008
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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-02-2008
Paul Lalli wrote:
> On Jan 2, 2:58 am, Ahmad <ahmad.abdulgh...@gmail.com> 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
 
Reply With Quote
 
Alan_C
Guest
Posts: n/a
 
      01-03-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression unicode character class trouble Diez B. Roggisch Python 2 09-05-2005 09:42 AM
RE: Help With EOF character and regular expression matching: URGENT Robert Brewer Python 1 02-23-2004 05:12 AM
Re: Help With EOF character and regular expression matching: URGENT Eric @ Zomething Python 0 02-23-2004 01:17 AM
XSD: Character in regular expression MBow XML 1 01-15-2004 12:03 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57