Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Matching duplicates in two strings or arrays and removing them both.

Last post 11-18-2008, 2:37 PM by m_s_ifland. 7 replies.
Sort Posts: Previous Next
  •  11-18-2008, 11:32 AM 48452

    Matching duplicates in two strings or arrays and removing them both.

    Hey guys and gals, brand new to regex language. Using Visual Studio DotNet. Writing an anagram program. I saw some stuff on regex and I would be intrerested in seeing how it would work it this situation if anyone has the time.

    I have two strings: 1. TextBox.text

                               2. The alphabet in english upper case A - Z

    Goal: To find the letters from the textboxt.text string that match the letters in the alphabet string and create a string of only the letters that didn't appear in both strings and turn it into an array.

    Then I would like to add a % symbol on either side of each letter in the new array.

    examples: Textbox.text = "trees" , Alphabet = String "ABCDE...RST...YZ"

    New array %A%, %B%, %C%, %D%, %F%,....%P%, %Q%, %U%, %V%,.....%Z%

     

    I don't know if this is possible with regex, but I would like to see if it is.

     

    Thank you,

    Michael.

  •  11-18-2008, 12:07 PM 48455 in reply to 48452

    Re: Matching duplicates in two strings or arrays and removing them both.

    It's not exactly the standard use of regex, but in any case:

    http://pastebin.com/f770f7ce5


  •  11-18-2008, 12:26 PM 48457 in reply to 48452

    Re: Matching duplicates in two strings or arrays and removing them both.

    This is more a programming task and not a regex only task. And as I always point out you are free to use a regex within your code, you'll have to write your own coding logic to accomplish your task.  http://regexadvice.com/blogs/mash/archive/2007/06/01/Are-you-ready-for-regex_3F00_.aspx

    For one you'll have to build your regex dynamically which can be tricky if you can't controll the input as some characters have special meaning in regex.

    Taking your sample if you create the regex pattern

    (?![trees])[A-Z]

    with the ignorecase option on

    and use the replacement pattern of

    %$0%

    and do a replace, then take that result and do a regex split with the pattern

    \B|[trees]

    again ignorecase option on

    You'll get an array close to what you what.  There will be a null element in the begining and end of the array but that will get you close to your goal.

    And just FYI I what to point out that [trees] is inefficiently written because the character class only match one  of the letters contained within it. It would be better write [tres] or [erst] because the second e is redundant and the character order is irrelevant.  I point this out because a lot of people mistakenly believe you can put patterns with a character class but it is actually just a list of characters.  But if your making this pattern dynamically it could easily have redundant characters listed.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-18-2008, 12:42 PM 48459 in reply to 48455

    Re: Matching duplicates in two strings or arrays and removing them both.

    ddrudik, thanks a bunch man.

    Couple of things. I'm using .NET for my application not C. But I was able to modify it to .NET.

    Second thing: How do I reference the new list?

    The "Console.WriteLine" statement obviously writes the new list somewhere, I just don't know where. I did a msgbox inside the for loop and it didn't show the letters with the"%" symbol next to it. But it did have the correct letters. I guess what I'm trying to say is that I will use the final list as an SQL query of what letters not to use in a search, i.e. the new list from "Console.WriteLine" 

    I am sorry, I'm still a little new to coding.

    Thank you very much,

    Michael

  •  11-18-2008, 1:56 PM 48462 in reply to 48459

    Re: Matching duplicates in two strings or arrays and removing them both.

    http://pastebin.com/m31b85aa

    try the above code; look for array *result* that holds all of your characters embraced in %.

    To avoid confusion, the solution ddrudic sent you was in C#, not in C, so you can plug it directly into your .NET solution.

    Sergei Z

  •  11-18-2008, 2:12 PM 48463 in reply to 48462

    Re: Matching duplicates in two strings or arrays and removing them both.

    Sergei Z, I really appreciate your help. I'm sorry about the confusion from C and C#. I'm using VB.NET. I'm can't understand the code that you've written. Is there any way that you can translate that into .NET for me?

    I'm also going to refernce the list later in the code, but after the 'Next' statement it doesn't allow reference to anything that was in that loop.

     

    Thank you,

    Michael.

  •  11-18-2008, 2:29 PM 48464 in reply to 48459

    Re: Matching duplicates in two strings or arrays and removing them both.

    m_s_ifland:

    I'm using .NET for my application not C. But I was able to modify it to .NET.

    That was C#.NET code.  Here it is in VB.NET:

    http://pastebin.com/f56a9274d

    It's a console application for purposes of demo, how to code an application outside of the bit of regex I supplied is something outside of the scope of what I can supply.


  •  11-18-2008, 2:37 PM 48467 in reply to 48464

    Re: Matching duplicates in two strings or arrays and removing them both.

    Yes, ddrudik, Than you. I was able to translate that part into VB.NET, thank you.

    It's the latter part I'm have trouble with. Displaying the final list to access late in the code.

     

    Thank you very much for your help so far,

    Michael

     

View as RSS news feed in XML