Click to See Complete Forum and Search --> : The Woe of Regular Expressions


Nicodemas
06-15-2006, 01:30 PM
Recently I've delved into a world of utter despair. The world of VBScript's regular expressions.

Can anyone here form a regular expression that can pluck out the word 'the' from a string using VBScript's RegExp object? Take for instance:

"The quick, brown fox jumped over the lazy dog."


I have tried. But as far as I know, the RegExp object doesn't let you look for entire words consisting of a solid string and exclude them.

I tried this: [^(T|t)he]

But that will knick any occurrence of T,t,h,e in the entire string. Using the previous string as an example, the expression would return:

"quick, brown fox jumpd ovr lazy dog"

russell
06-15-2006, 02:39 PM
<%
Dim r
Dim str

str = "The Quick brown fox jumped over the lazy dogs"

'' Regular Expression method of replacing
Set r = New RegExp

With r
.Pattern = "the"
.IgnoreCase = True
.Global = True
End With

Response.Write r.Replace(str, "") & "<br><br>"

Set r = Nothing


'' And how to do it without Regular Expressions
'' initial string
'' string to replace
'' the replacement string
'' how many instances to replace (-1 means all)
'' case sensitivity -- 0 case sensitive, 1 means case insensitive
Response.Write Replace(str, "the", "", 1, -1, 1)
%>

Here's a good reference (http://authors.aspalliance.com/brettb/VBScriptRegularExpressions.asp)