Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi gr8 ppl

$_ = "package daa; package MyClass::two; package Myclass::SubClass::t +hree;"; /^(\w+) (\w+)\;/; print $2;

The above code works when given as  package daa The same code will not work if the string is like this

$_ = "package MyClass::Subclass::one; package Myclass::two;" (please note the semi-colon after the module name)

I got struck here now.. Can anyone give a regex which will solve such kind of a string

Thank u

Replies are listed 'Best First'.
Re: Regular Expression
by prasadbabu (Prior) on Dec 07, 2005 at 13:46 UTC

    \w won't match ':', it will match only [0-9a-zA-Z_].

    You try this

    $_ = "package MyClass::Subclass::one; package Myclass::two;"; /^(\w+) ([\w:]+)\;/; print $2;

    Also take a look at perlre.

    Prasad

Re: Regular Expression
by holli (Abbot) on Dec 07, 2005 at 13:49 UTC
    Why not simply
    @packages = map { s/package //; $_ } split /; */;
    ?


    holli, /regexed monk/
Re: Regular Expression
by PerlingTheUK (Hermit) on Dec 07, 2005 at 13:48 UTC
    \w does not match any colons. I guess you either want to include colons or you want to use \S which negates the whitespace.

    Cheers,
    PerlingTheUK
Re: Regular Expression
by merlyn (Sage) on Dec 07, 2005 at 19:43 UTC