PDA

View Full Version : Regular expressions



next
10-21-2010, 09:04 AM
I'm trying to use regex, but can't seem to get the pattern right. Here's my objective:
I need a pattern that will match any phrase that starts with "f", has zero or more spaces and has "ch" or letter combination very close to that. The length should be 17+ characters
I'm parsing this string: "furniture checklist"
this is my pattern so far: "^(f\b+)\s*((c?h)|(ce)|(h?c))\D$" it returns an error. If I remove \b, then it doesn't, but that loses "any word that starts with F".

Here's my complete code example:
Sub Macro3()
Dim RegEx As Object, x As String, result As Boolean
Set RegEx = CreateObject("vbscript.regexp")
x = "furniture checklist"

RegEx.Pattern = "^(f\b+)\s*((c?h)|(ce)|(h?c))\D$"
'RegEx.IgnoreCase = True
result = RegEx.Test(x)

Debug.Print result

End Sub

Help!

Sebastian H
10-21-2010, 06:55 PM
"^f.*\s.*((c?h)|(ce)|(h?c))\D" seems to do what you need.

Not sure what you have the "\D" for; it only excludes some exotic cases such as "furniture c4".