[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] - Regex help



On Thu, Oct 18, 2007 at 15:05:18 +0200, Andrea Ganduglia wrote:
> <?php
>     $str = 'I\'m a text: =match1 =match2=match3=match4 =nomatch{}
> =match5=match6';
>     //$rex = "/=[A-z]+/"; // works but not exclude =nomatch{}
>     $rex = "/=[A-z0-9]+(\s|=)/"; //
>     $N = preg_match_all($rex,$str,$out);
> 
>     print $N." $str\n";
>     print_r($out);
> ?>
> 
> Sorry for OT. I have a little problem with regex above. The script is
> in PHP, but regex should be compatible with Perl.
> 
> I want match =[A-z0-9] where char after this is \s or =, final = is
> the start char of next match. How can go back after first matching?

If the regex syntax of PHP is fully compatible then you can use
look-ahead assertion matches. Here is how that works in perl (the second
part adds a look-behind assertion to get rid of the leading "=" in the
same step):

#! /usr/bin/perl

$str = 'I\'m a text: =match1 =match2=match3=match4 =nomatch{}=match5=match6';

@array = $str =~ /=[A-z0-9]+(?=\s|=|$)/g;
foreach (@array) {print "$_\n"};

print "-----\n";

@array = $str =~ /(?<==)[A-z0-9]+(?=\s|=|$)/g;
foreach (@array) {print "$_\n"};

# the end

-- 
Regards,            | http://users.icfo.es/Florian.Kulzer
          Florian   |



Reply to: