[Israel.pm] Regex question

Gaal Yahas gaal at forum2.org
Sun Jan 25 09:31:25 PST 2009


You can always simplify (?:(?:foo)) to (?:foo).

On Sun, Jan 25, 2009 at 7:13 PM, Shmuel Fomberg <semuelf at 012.net.il> wrote:
> Thanks for the answer.
>
> What about the:
>                (?:
>                        # Allow both :: and ' in namespace separators
>                        (?: \' (?!\d) \w+ | \:: \w+ )
>                )*
>
> does the double parenthesis do something, or can it be collapsed to:
>
>                # Allow both :: and ' in namespace separators
>                (?: \' (?!\d) \w+ | \:: \w+ )*
>
> ?
>
> Shmuel.
>
>
>
> Yitzchak Scott-Thoennes wrote:
>> On Sat, January 24, 2009 4:40 pm, Shmuel Fomberg wrote:
>>> Is it correct that this regex:
>>>
>>> $content =~ /^(
>>> [\$@%&*]
>>> (?: : (?!:) | # Allow single-colon non-magic vars
>>> (?: \w+ | \' (?!\d) \w+ | \:: \w+ )
>>> (?:
>>> # Allow both :: and ' in namespace separators
>>> (?: \' (?!\d) \w+ | \:: \w+ )
>>> )*
>>> (?: :: )? # Technically a compiler-magic hash, but keep                                #it here
>>> )
>>> )/x;
>>>
>>> is the same is this?
>>>
>>> $content =~ /^(
>>> [\$@%&*]
>>> (?: : (?!:) | # Allow single-colon non-magic vars
>>> (?: \w+ | \' (?!\d) \w+ | \:: \w+ )
>>> )
>>> # Allow both :: and ' in namespace separators
>>> (?: \' (?!\d) \w+ | \:: \w+ )*
>>> (?: :: )? # Technically a compiler-magic hash, but keep it here
>>> )/x;
>>
>> No, the second one continues on after a single colon to look for a namespace
>> separator, etc, where the first one would have stopped.  So when $content
>> is "$:'a", one matches two characters and one matches all four.
>>
>> To show the difference more simply, you are asking if
>>
>>     ( A (?: B | C D ) )
>>
>> is the same as
>>     ( A (?: B | C ) D )
>>
>> Moving D makes it matched after B or C instead of just after C.
>>
>> _______________________________________________
>> Perl mailing list
>> Perl at perl.org.il
>> http://perl.org.il/mailman/listinfo/perl
>>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list