Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Regexp matching words, not doing what I expect

by graff (Chancellor)
on May 14, 2009 at 03:11 UTC ( [id://763980]=note: print w/replies, xml ) Need Help??

in reply to Regexp matching words, not doing what I expect

You should consider ditching the regex approach and using a module: Text::Ngrams looks like the right one for you (not to be confused with Text::Ngram -- note the "singular" -- which only works on character ngrams, not word ngrams).

If you have some strange compulsion not to use a module, you should still consider ditching the regex -- split the string into an array of words and use a for loop to output your word pairs:

my @words = split " ", $text; print "$words[$_-1] $words[$_]\n" for ( 1 .. $#words );
(updated to fix bone-headed error in for loop)

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-06-16 08:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.