Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: regex issue

by Anonymous Monk
on Aug 03, 2016 at 16:49 UTC ( #1169092=note: print w/replies, xml ) Need Help??

in reply to regex issue

i made a mistake it should be length($_)==3.

Replies are listed 'Best First'.
Re^2: regex issue
by ExReg (Priest) on Aug 03, 2016 at 18:20 UTC

    A slightly different approach can give you what you are looking for. By using a lookahead, you can do what you want.

    $term = 'Dit is het eerste het is niet het laatste Dit'; @captured = $term =~ /\b(\w\w\w)\b(?=.*\1\b)/g; print join ' ', @captured; _____________ Dit het het

    The \b are word boudaries (change from letter/number/underscore) to non-letter/number/underscore or vice-versa.

    The (?= looks forward for what comes after it, but remembers where it starts.

    The \1 is the same as your \g1 (I unfortunately have an older perl.)

    The g at the end means capture them all

    het appears twice since it is there three times

      Small correction, you missed a word-boundary assertion in the look-ahead. The regex should be:
      Without the additional \b before \1, three-letter words that are trailing substrings of other words would also match.

        Thanks. I missed that copying from the PC that has perl that I tested this on to this one.

      Thank you

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-11-27 11:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found