tags:

views:

532

answers:

4

Since VBScript doesn't support lookbehinds, I'm looking for an alternative solution.

I have the string '\E\F\'.

I want to replace \F\ with '~', but ONLY if it is not preceded by a \E.

After the substitution, I want '\E\F\' to be '\E\F\'.

If the string was 'randomText\F\', I would want it to look like 'randomText~' after the substitution.

Solution:

I just decided to StrReverse it and do a negative forward lookahead. It's not the most elegant solution, but it seems to work in this case.

Dim regEx, str1
str1 = StrReverse("The quick \F\ brown \E\F\ dog.")
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Pattern = "\\F\\(?!E\\)"
regEx.Global = True
ReplaceTest = regEx.Replace(str1, "%")
+1  A: 

you want a negative lookbehind

(?<!\\E)\\F\\
ax
Though I think you need an extra \\ at the end
Simon Nickerson
yeah right, thank you. updated.
ax
VBScript (and JavaScript and JScript) regular expressions don't support lookbehind (http://www.regular-expressions.info/vbscript.html). you might find some alternative googling for "VBScript Lookbehind". or change to a language that does support it (http://www.regular-expressions.info/refflavors.html , search for "Lookaround")
ax
A: 

You're looking for a "negative lookbehind assertion", which looks like this (Python example):

DATA = "\\E\\F\\ or randomText\\F\\"
import re
print re.sub(r'(?<!\\E)\\F\\', '~', DATA)
# Prints \E\F\ or randomText~
RichieHindle
+2  A: 

VBScript doesn’t support look-behind assertions. But try this:

(^.?|[^\\].|\\[^E])\\F\\

Or this:

(^.?|(?!\\E)..)\\F\\

Replace the match with $1~ (first submatch and ~).

Here’s an explanation: In general there are two situations: If there is no or just one character before \F\ (^.?), everything is ok. But if there are at least two characters before \F\, we need to make sure, that these characters are not \E. So we say, that the two preceeding characters are either

  • any character except \ followed by any arbitrary character ([^\\].), or
  • \ followed by any character other then E (\\[^E]).

That construct ensures that every other combincation except \E is allowed.

The same applies to the second expression. But here we use the negative look-ahead assertion to ensure that the two characters before \F\ is not \E.

Gumbo
The quick \F\ brown FF\E\F\ dog. Turns to The quic~ brown FF\E\F\ dog.
Chris
@Chris: You need to replace the match with the first submatch and `~`, so `$1~`.
Gumbo
Sorry. Missed that. They seem to work at first glance. Let me try to decipher them and do some testing. Then, I'll give you the credit.
Chris
Nice one! Or should I say "Nice *two*"? :) I would have left out the '?' in `\\[^E]?` but it seems to work either way.
Alan Moore
They seem to work. If you could give a quick summary of one of them, I'd appreciate it. I have some knowledge of regex, but I'm struggling to understand the solutions completely.
Chris
A: 

don't have to use regex. here's one solution

strString = "The quick \F\ brown \E\F\ dog"
s = Split(strString," ")
For i=1 To UBound(s)
    If Not InStr(s(i),"\E\F")> 0 And InStr(s(i),"\F") > 0 Then
     s(i) = "~"
    End If 
Next
strFinal=Join(s," ")
WScript.Echo strFinal

output

C:\test>cscript /nologo test.vbs
The quick ~ brown \E\F\ dog
ghostdog74