[ale] PHP -> Perl
Dylan Northrup
docx at io.com
Mon Apr 21 08:56:51 EDT 2003
Calvin Harrigan wrote:
>Hi all,
> I've been fooling around with converting a bit of PHP code to PERL.
>Not being a user of either language I was able to get everything
>converted except the following:
>
><PHP CODE>
>while(!preg_match("/^(\d+)\s+(\S+)$/", $retstr, $matches))
>{
> #do stuff
>}
>
></PHP CODE>
>
>I have not been able to mimic this functionality in perl. I have gotten
>as far as:
>
><PERL CODE>
>while (not ($retstr =~ /^(\d+)\s+(\S+)\1\2$/g ))
>{
> #DO STUFF;
>}
>$matches[1] = $1;
>$matches[2] = $2;
></PERL CODE>
>
>This seems to almost work but I'm missing something. I have been
>scouring the web all afternoon, but just can't finish it up, any input
>would greatly be appreciated.
>
Your regular expression is different in the perl example than in the php
example. The RE for perl will match an arbitrary amount number of
digits, followed by an arbitrary number of spaces/tabs, followed by an
arbitrary number of non-spaces/non-tabs, followed by the sub-string that
was matched during the "arbitrary number of digits" match, followed by
the sub-string that was matched by the "arbitrary non-spaces/non-tabs"
match.
So, here are a few strings to test on . . .
$str1='123 marzipan';
$str2='234 strongbad234strongbad';
The first string, $str1, would not match. It's got numbers followed by
a space followed by some non-space characters, but it doesn't have the
digits/characters that follow. The second string, $str2, would match
because it has the repetition of the matched sub-strings. Also, I'm not
sure what your data set is so I'm not sure whether or not the 'g' on the
end is necessary. And since you don't state the failure case of the
above perl code, it's hard to diagnose exactly how to correct it. . . if
you could provide some example data and expected output, that'd make
diagnosis and suggesting solutions much easier :-)
--
Dylan Northrup <*> docx at io.com <*> http://www.io.com/~docx/
"Easy to bitch, easy to whine, easy to moan, easy to cry, easy to feel
like there ain't nothing in your life. Harder to work, harder to
strive, hard to be glad to be alive, but it's really worth it if you
give it a try." -- Cowboy Mouth, 'Easy'
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list