PDA

View Full Version : Solved: Combining Lookahead and Lookbehind in regular expressions



Lammutaja
12-02-2005, 05:43 AM
Hello!
Can somebody help me how i can combine Lookahead and Lookbehind
in regular expressions using VBA?

Example regular expression has been taken from http://www.samspublishing.com/title/0672325667#
(Section "Combining Lookahead and Lookbehind")

Code which did not work: :banghead:

Sub Try()
Const str = "<TITLE>Ben Forta's HomePage</TITLE>"
Const regulexpr = "(?<=<[tT][iI][tT][lL][eE]>).*(?=</[tT][iI][tT][lL][eE]>)"
Debug.Print ReplaceText_RegExp(str, regulexpr, "OK!")

End Sub

Function ReplaceText_RegExp(RepString As String, patrn As String, replStr As String) As String
Dim regEx ' Create regexp variable.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
ReplaceText_RegExp = regEx.Replace(RepString, replStr) ' Make replacement.
End Function

mvidas
12-02-2005, 10:58 AM
Hi Lammutaja,

I don't believe VB's regexp actually has lookahead/lookbehind functionality, something I know comes with most other regexp's (though vb.net may).

However, you can still get similar functionality using vb/vba's version.

First off, you don't need to do such things as [tT] when you can just set the .IgnoreCase property to true.

Instead of doing lookbehind/lookahead, you can just put those elements as submatches, then include them in your replace (using $1 and $2 for the first two submatches in the replstr):Sub Try()
Const str = "<TITLE>Ben Forta's HomePage</TITLE>"
Const regulexpr = "(<title>).*?(</title>)"
Debug.Print ReplaceText_RegExp(str, regulexpr, "$1OK!$2")

End Sub

Function ReplaceText_RegExp(RepString As String, patrn As String, replStr As String) As String
Dim regEx ' Create regexp variable.
Set regEx = CreateObject("vbscript.regexp") ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True
ReplaceText_RegExp = regEx.Replace(RepString, replStr) ' Make replacement.
Set regEx = Nothing
End FunctionI'm guessing that was what you were trying to accomplish (change the title from 'Ben Forta's HomePage' to 'OK!'). I must admit I have never used any kind of lookahead or lookbehind, any information I know about it comes from http://perl.active-venture.com/pod/perlretut-lookahead.html which I just looked up because of this question :) But it seems that is what the function is, just a way to specify what comes before a string and after a string without including it in the match. Another way to do this would be to have the center portion (the page title) as a submatch as well, if you wanted to refer to only that:Sub Try()
Const str = "<TITLE>Ben Forta's HomePage</TITLE>"
Const regulexpr = "(<title>)(.*?)(</title>)"
Debug.Print ReplaceText_RegExp(str, regulexpr, "$1OK!$3")
End Sub

Function ReplaceText_RegExp(RepString As String, patrn As String, replStr As String) As String
Dim regEx ' Create regexp variable.
Set regEx = CreateObject("vbscript.regexp") ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True
If regEx.Test(RepString) Then 'if it matches, debug the original page title
Debug.Print regEx.Execute(RepString).Item(0).SubMatches(1)
End If
ReplaceText_RegExp = regEx.Replace(RepString, replStr) ' Make replacement.
Set regEx = Nothing
End Function
Matt

Lammutaja
12-05-2005, 12:45 AM
Thank You!!! :clap:

J?ri.

mvidas
12-05-2005, 12:50 PM
Glad to help! Let me know if you need anything else.

I'm going to mark this thread solved to help keep the forum clean, in the future you can do this yourself by going to Thread Tools at the top of the page, then selecting "Mark Thread Solved".

Matt

brettdj
12-05-2005, 04:48 PM
Hi Lammutaja,
I don't believe VB's regexp actually has lookahead/lookbehind functionality, something I know comes with most other regexp's (though vb.net may).


Nope - and that is sooooo frustrating.

mvidas
12-06-2005, 07:22 AM
Nope - and that is sooooo frustrating.
What gets me is the non-inclusion of the 'anything but ____' idea for the pattern

Have you played with it in vb.net? What I've seen so far seems to be a bit easier, but I haven't really played around all that much yet