Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: How do you find duplicates in a string?

by davido (Cardinal)
on Sep 21, 2006 at 16:20 UTC ( [id://574180]=note: print w/replies, xml ) Need Help??


in reply to How do you find duplicates in a string?

You can use regular expression capturing, and backreferences.

if( $string =~ m/ \b([[:alpha:]]+)\b # Match and capture word .*? # Skip what you don't need \b\1\b # Match the captured word /x ) { print $1, "\n"; }

Limitation: Words can contain only alpha characters. You could modify the expression: [[:alpha:]] so as to include what you might consider to be legal word characters, such as ' (apostrophe) and - (hyphen). I used the /x modifier to facilitate grouping the regular expression's sub-expressions into meaningful clusters so that it's easier to read.

Hope this helps!


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found