Question : String extraction with regex

Access 2007
This regex function came from matthewspatrick.  I'm trying to add my own regex to it like this

RegExpReplace("GRAND BLANC TOWNSHIP", "(? should return GRAND BLANC TOWNSHIP

RegExpReplace("GRAND BLANC TOWNSHIP", "(? GRAND BLANC test

The problem is I'm getting an error of
Run-Time error '5017'
Application-defined or object-defined error.

Any idea whats causing this?  I have VBScrip Regex set as a reference..

Thanks
Keith
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True)
 
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll controls
    ' whether all instances of the matched string are replaced (True) or just the first instance (False)
   
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
   
    ' If you use this function from Excel, you may substitute range references for all the arguments
   
    Dim RegX As Object
   
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
    End With
   
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
   
    Set RegX = Nothing
   
End Function
 
 
and then use it like this:
 
UPDATE YourTable
SET YourField = RegExpReplace([YourField], " {2,}", " ")

Answer : String extraction with regex

Try

    ? RegExpReplace("GRAND BLANC TOWNSHIP", "\btown\b", "test",,False)

You need to turn off MatchCase (default of your function is True), and you can use \b to match "word boundaries". See:

[RegExp] Pattern Property
http://msdn2.microsoft.com/en-us/library/f97kw5ka(VS.85).aspx

(°v°)
Random Solutions  
 
programming4us programming4us