Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: pattern sequence dispersed within text

by choroba (Bishop)
on Oct 01, 2012 at 11:41 UTC ( #996626=note: print w/replies, xml ) Need Help??

in reply to pattern sequence dispersed within text

This should do what you want:
#!/usr/bin/perl use warnings; use strict; use feature 'say'; my $word = 'helen'; my $input = 'xxxhexxxxxxxx xle xxnxle nxxx xxnxxx xnxxhexx nxxxxx xlex +xxxxx nxnx xxxx'; my $regex = '([^ \b]*' . join('[^ \b]*).*?([^ \b]*', $word =~ /(..?)/g +) . '[^ \b]*)'; my @matches = $input =~ /$regex/g; say for @matches;
The regex is constructed from the $word dynamically, so you do not have to know the length of it in advance.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: pattern sequence dispersed within text
by Anonymous Monk on Oct 01, 2012 at 11:53 UTC
    Just in case quotemeta insert  map { "\Q$_\E" }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996626]
[Corion]: choroba: Yeah - I found that if you have the "right" datastructure from the start, it's fairly easy, but if you have some global datastructure that you mutate, you have to rewrite a lot to pass around the results instead.
[Corion]: I find the process quite similar to (in Prolog) passing around an accumulator variable in recursive predicates

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2017-12-11 11:23 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (289 votes). Check out past polls.