Results 1 to 3 of 3

Thread: [2005] Finding possible matches

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    [2005] Finding possible matches

    Say I have a textfile with the following words:

    Apple
    Bake
    Bike
    Cake
    Lemon
    Water


    I want to find all the four letter words with the second letter of 'a' and the fourth letter of 'e'. Something like '_a_e'

    The matches would be Bake and Cake. How would I find the matches though? I do not know what to search for. I have add the list to a String() and have also tried a List(Of String), but cannot figure it out?

    Any help is appreciated. Thanks, Troy.
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Finding possible matches

    Regex is your friend for doing stuffs like this. The pattern for finding 4 letter words is "\b[a-zA-Z]{4}\b", however, since you only want 4 letter words that have a in the 2nd position and e in the last position, you need to modify the pattern. Let me give a quick explanation:
    [a-zA-Z]{1} : match any letter in the groups a-z and A-Z exactly 1 occurence
    [a-zA-Z]{1}a : same as above plus followed by an a
    [a-zA-Z]{1}a[a-zA-Z]{1}e : match (any letter)a(any letter)e
    Add \b to the beginning and the end of the pattern and you have what you need as in the code below.
    Code:
    Dim input As String = "Apple Bake Bike Cake Lemon Water nightmare scare are"
            Dim pattern As String = "\b[a-zA-Z]{1}a[a-zA-Z]{1}e\b"
            Dim rgx As System.Text.RegularExpressions.Regex
            Dim matches As System.Text.RegularExpressions.MatchCollection
            matches = rgx.Matches(input, pattern)
            'View output
            Dim output As String = String.Empty
            For Each m As System.Text.RegularExpressions.Match In matches
                output &= m.Value & " "
            Next
            MessageBox.Show(output)

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Finding possible matches

    Regular Expressions had always intimidated me, but your example has put it into perspective.

    I will try it out. Thanks.
    Prefix has no suffix, but suffix has a prefix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width