Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting duplicate in string?

Status
Not open for further replies.

cleansedbb

Technical User
Feb 11, 2002
95
US
I need help deleting duplicates but I want to allow only one dupe.

i.e. very very is allowed but very very very isnt?

this is what I'm using so far. and it deletes the dupe but it deletes all dupes so I cant have a double.

$FORM{'message'} =~ s/(\b\S+)(\s+\1\b)+/$1/g;

Thanks for any input
 
also wondering how to check for word pairs or groups.

i.e. "this is a test" instead of just looking to see if the previous word matches.

Thanks again.
 
I don't have time to play with it, but, I think you can extent your approach a little with a positive look ahead. The chunk below shows the positive look ahead checking to see what is coming next. The first while(//) prints everytime it sees the word 'text'. The second while(//) only print once as the positive look ahead for 'text' is only satisfied once.

Code:
#!/usr/bin/perl
my $str = 'some text text to play with and the word text is followed by a twin the first time and is alone the second time.';
while ($str =~ /text/g)
  {
  print "\nSimple Regex Matched: $&\nwhich is followed by: \n--> $' <--\n";
  }
  print "\n\n";
while ($str =~ /text (?=text)/g)
  {
  print "Pos Look Ahead Matched: $&\nwhich is followed by: \n--> $' <--\n";
  }

HTH

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top