Regular expressions duplicates

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Hi,
I have a list of comma sepertaed numbers e.g.

287,456,287,876,877,877

I want to use regualr expressions to numbers appear once and only once
in the list i.e. for the example above....

287,456,876,877


can any one help me with the pattern and commands that I should use.


Thanks,
Nick
 
Iam no expert of regular exproessions but I think you cannot do this with
regex.
But what you can do is string.Split(",") on the input string and use foreach
loop and put all values in a hashtable if
hashtable.containsvalue(str)==false.
hope that helps.
 
Nick said:
I have a list of comma sepertaed numbers e.g.

287,456,287,876,877,877

I want to use regualr expressions to numbers appear once and only once
in the list i.e. for the example above....

287,456,876,877


can any one help me with the pattern and commands that I should use.

Any reason for particularly wanting to use regular expressions? I'd
just use String.Split, keep a hashtable of values you've already seen,
and build up the string again.
 
I think replacing every occurence of the pattern
"((^|,)(?<Number>(?>\d+)))(?<=\k<Number>.*\k<Number>)" with "" would do what
you want.
However you should know that this is definitely A LOT slower than any plain
C#-approach (especially on longer strings, this is O(n²)), like using Split.
If there is no special reason for using regular expressions in your case,
you probably shouldn't use them.

Niki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top