Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Perl Pattern Matching & RegEx's

by choroba (Canon)
on Mar 07, 2013 at 14:29 UTC ( #1022243=note: print w/ replies, xml ) Need Help??


in reply to Perl Pattern Matching & RegEx's

The main problem is matches in /g cannot overlap. This can be solved by using look-ahead, though:

#!/usr/bin/perl use warnings; use strict; use feature qw(say); my $string = 'AAAbcdAAAdcbAAAbbdAAAxAAAA'; my $delimiter = 'AAA'; my @positions; push @positions, pos($string) while $string =~ /(?=$delimiter)/g; for my $from (@positions) { for my $to (grep $_ - length $delimiter > $from, @positions) { say substr($string, $from, $to - $from) . $delimiter; } }

Update: Typo fixed. Thanks jaiieq, damn netbooks.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Perl Pattern Matching & RegEx's
Download Code
Re^2: Perl Pattern Matching & RegEx's
by jaiieq (Novice) on Mar 07, 2013 at 14:45 UTC
    This is exactly what I needed. Which also gives me the ability to easily change the delimiter to say 'AA' and produce the output I need. Thank you.

    There is a small typo in your code as you have a quote after $delimiter in the say line

Re^2: Perl Pattern Matching & RegEx's
by Dallaylaen (Monk) on Mar 07, 2013 at 15:34 UTC
    Wow, substr is so much better here than split/join!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022243]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2015-07-05 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls