|
-
Jul 12th, 2007, 12:56 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Jul 12th, 2007, 07:38 AM
#2
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)
-
Jul 12th, 2007, 11:33 AM
#3
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|