Oh Great 'gods' of Regular Expressions-Help!

  • Thread starter Thread starter Schorschi
  • Start date Start date
S

Schorschi

This one has got me bam-boz-ald! or however your spell it...

I have a sequence of text for example, "One Two Three Four Three Two
One Three", and I need a regular expression, if possible, to remove
the duplicates, so I would end with a string, "One Two Three Four."
1, "One" was removed, 1, "Two" was removed, 2, "Three" are removed,
and the single "Four" instance is untouched. If you notice, the first
instance of each word, is already in proper sequence. So, removing
the duplicates is done after the initial word is identified. Another
example is "10 8 8 5 3 7 10 9 8 5", with the result as "10 8 5 3 7 9."

Anyone try to do this before?
 
a possible way to do this
split your string to a string array on the space
add the elements of the array 1 by 1 to an array list using the
arraylist.contains to c if the item already exists

hope it helps
eric
 
Not sure why you'd want to use regular expressions for this. I'm pretty
good at regex, and this one is not something I'd code that way.

Just parse the strings (see String.Split(' ') ) into an array, and insert
individual items into a sorted ArrayList structure (for short lists, simply
scan for dups... if the list is going to be long, use a binary search.
Either way, insert into the correct location if not found).

I hope this helps,
--- Nick
 
I would have to agree with the other posters that regular expressions are
probably not the right tool in this case. That being said, I found the
problem quite intiguing. I have found a way to accomplish it, but it
requires more than just one simple expression. You have to reverse the
input and then reverse the result for it to work. Here is the code:

Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic

Module Module1
Sub Main()
Dim re As New Regex("\b(?<a>\w+)\b ?(?=.*\b\k<a>\b.*)")
Dim sReplacment As String = ""
Dim sInput As String = "10 8 8 5 3 7 10 9 8 5"
Console.WriteLine(StrReverse(re.Replace(StrReverse(sInput),
sReplacment)))
Console.ReadLine()
End Sub
End Module

Maybe someone else can modify this to work using only a single expression.


Brian Davis
www.knowdotnet.com
 
Brian, gets the gold star this time. Everyone is right, a simple sort
removing the duplicates, or a compare-parse works, but did not seem
the most elegant solution.

Thanks for the responses!
 
Hi Schorsi,

You had better look to the complete message from Brian,
It was for him a challenge, not the best or most elegant solution.

It is probably very much more time consuming than the other solutions

The only good thing is that it is a very good build in obfuscator.
But that is something I mostly don't see as a benefit.

Just a thought Cor

Cor
 
Back
Top