[thelist] Re: Regex remove repeats

Burhan Khalid thelist at meidomus.com
Thu Jan 27 01:55:10 CST 2005


Chris Nicholls wrote:
>  >>I'm stuck on writing a regex that is going to lok ahead to see if the
>  >>next item in the comma separated list is the same as the last and, if 
>  >>it is, to remove it
> 
> This isn't a regex, but one solution, simpler and more scalable, 
> depending on your scripting language, would be:
> 
> 1. Split string into list
> 2. Create an empty "holding" array. Also create an empty hash/struct
> 3. Loop through list see if value exists as a key in your new struct.
>    If not, add value to holding array, and set flag in struct saying 
> value has been "seen"
> 4. Write holding array, which now contains only unique values, back out 
> to new list.
> 
> In Perl:

[ trimmed ]

I'm not sure I understood the entire step 3, but to me it seems like you 
need to find uniques in a set of items that are originally in the format 
"a,b,c,3,df,er".  The following is a PHP solution to the problem (as I 
understand it).

In PHP :

<?php

   //Step 1
   $contents = explode(",",$string);
   //Steps 2 & 3
   $unique_array = array_unique($contents);
   //Step 4
   $unique_list = implode(",",$unique);
?>


More information about the thelist mailing list