I try to match 01010 with /010/ but there is one return (01010) for this match. There are actually two match (010-is common-10) in that string with regex. I used /010/g but againit returns just one match. Is it possible to match this regex twice with such string?

Recommended Answers

All 2 Replies

I don't see how to match 010 twice but you could match 010 and 10 by using /.*?10/.

#!/usr/bin/perl
use strict; 
use warnings;

my $str = '01010';

#When 10 found, 'lookbehind' to see if 0 precedes it.
#If 0 precedes the 10 then capture the 10
#You can do this for repeated matches.
my @results = $str =~ m/(?<=0)10/g;
print join ', ', @results, "\n";

#If you want 010 010 instead of 10 10 you need to fix the results
#by prepending a 0 to each 10.
@results = map 0 . $_, @results;
print join ', ', @results, "\n";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.