PDA

Click to See Complete Forum and Search --> : Regex Help matching


TheUsed
Nov 17th, 2011, 01:36 PM
I have a few sentences that contain a set of words. The regex pattern must match two words in order to be true. But I have a combination of 3 words to be matched that are case insensitive.

The following expression does a great job... but not quite. I wish to shorten the expression by getting rid of the last group and instead having the first group validate the last group. (I have the last group to match any you or your after the hum.)

(\b[yY]ou(r)?\b.*){1,2}(\bhum(ming)?\b(\byour?\b.*)?)

Here are the sentences it needs to match within.

Your continue to hum your song.

You continue to fumble slightly as you hum a confident lullaby.

You finish humming a confident lullaby.

You continue to hum a confident lullaby with only the slightest hint of dofficulty.

If the pattern above matches.. I need it to then grab the areas where it didn't match and ONLY those areas.

wild_bill
Dec 19th, 2011, 04:40 PM
Like this?

Dim pattern = "\b(your?)\b(.*)\b(hum(?:ming)?)\b(.*)"
Dim rx As New Regex(pattern, RegexOptions.IgnoreCase)

Dim phrases = New String() { _
"Your continue to hum your song.", _
"You continue to fumble slightly as you hum a confident lullaby.", _
"You finish humming a confident lullaby.", _
"You continue to hum a confident lullaby with only the slightest hint of dofficulty."}

For Each phrase In phrases
Dim m = rx.Match(phrase)
Debug.WriteLine(m.Groups(1).Value)
Debug.WriteLine(m.Groups(2).Value)
Debug.WriteLine(m.Groups(3).Value)
Debug.WriteLine(m.Groups(4).Value)
Debug.WriteLine("")
Next