tags:

views:

139

answers:

5

I'm searching for strings within strings using Regex. The pattern is a string literal that ends in (, e.g.

# pattern
" before the bracket ("

# string
this text is before the bracket (and this text is inside) and this text is after the bracket

I know the pattern will work if I escape the character with a backslash, i.e.:

# pattern
" before the bracket \\("

But the pattern strings are coming from another search and I can not control what characters will be or where. Is there a way of escaping an entire string literal so that anything between markers is treated as a string? For example:

# pattern
\" before the ("

The only other option I have is to do a substitute adding escapes for every protected character.

A: 

The following regex will capture everything from the beginning of the string to the first (. The first captured group $1 will contain the portion before (.

^([^(]+)\(

Depending on your language, you might have to escape it as:

"^([^(]+)\\("
Amarghosh
I don't think this is an actual answer to the question: the literal that is searched for by regex comes from an external source and can contain characters that need to be escaped because they have meaning in regex.
peSHIr
exactly, I need to effectively insert \ in front of every character in the string. how do I do that in Regexp VBA?
Mark
+1  A: 

You didn't specify the language, but it looks like Python, so if you have a string in Python whose special regex characters you need to escape, use re.escape():

>>> import re
>>> re.escape("Wow. This (really) is *cool*")
'Wow\\.\\ This\\ \\(really\\)\\ is\\ \\*cool\\*'

Note that spaces are escaped, too (probably to ensure that they still work in a re.VERBOSE regex).

Tim Pietzcker
A: 

Hi Tim,

re.escape is exactly what I need. I'm using regexp in Access VBA which doens't have that method. I only have replace, execute or test methods.

Is there a way to escape everything within a string in VBA?

Thanks

Mark
+1  A: 

Maybe write your own VBA escape function:

Function EscapeRegEx(text As String) As String
    Dim regEx As RegExp
    Set regEx = New RegExp

    regEx.Global = True
    regEx.Pattern = "(\[|\\|\^|\$|\.|\||\?|\*|\+|\(|\)|\{|\})"

    EscapeRegEx = regEx.Replace(text, "\$1")
End Function
Phydaux
Perfect! Exactly what I needed. Thanks!
Mark
A: 

I'm pretty sure that with the limitations of the RegExp abilities in VBA/VBScript, you are going to have to replace the special characters in your pattern before using it. There doesn't seem to be anything built into it like there is in Python.

KevenDenen
Thanks for getting back. I ended up writing a wrapper round replace to escape all special characters:Public Function Escape(ByVal strString As String) As StringDim re As New RegExpDim EscapeChars() As Variant EscapeChars = Array("\\", "\*", "\+", "\?", "\|", "\{", "\[", "\(", "\)", "\^", "\$") For Index = 0 To UBound(EscapeChars) re.Pattern = EscapeChars(Index) re.Global = True strString = re.Replace(strString, EscapeChars(Index)) Next Escape = strStringEnd Function
Mark