Hi,

I am trying to find a pattern that allows me to avoid words within
quotations, eg "word1 word2 word3"
Word outside (without quotation) needs to have quotes added.

simplified as:

regEx.Pattern = "\b" & keyword & "\b"
regEx.Global = true
strWord = regEx.Replace(strng, chr(34) & keyword & chr(34))

input. word1 word2 word3 "word1 word2 word3"
output: "word1" "word2" "word3" "word1 word2 word3"

the patter to match string within quotes is """.*?""" ' but I need to have
it ignored.

Thanks in advance
Christian

Re: Reqular expression quiz by Steve

Steve
Wed Jan 25 18:00:33 CST 2006

Christian Perthen wrote:

> I am trying to find a pattern that allows me to avoid words within
> quotations, eg "word1 word2 word3"
> Word outside (without quotation) needs to have quotes added.
>
> input. word1 word2 word3 "word1 word2 word3"
> output: "word1" "word2" "word3" "word1 word2 word3"

You need to split the problem into two pieces: (1) find the substring
outside the quotes, and (2) delimit the keywords in that substring.

Str = "word1 word2 word3 ""word1 word2 word3"""

Set RE1 = New RegExp
' Break the string into unquoted and quoted parts
RE1.Pattern = "([^""]*)("".*?"")?"
RE1.Global = True

Set RE2 = New RegExp
' Find kewords
RE2.Pattern = "\bword[123]\b"
RE2.Global = True

Function Delimit (Match, Unquoted, Quoted, Index, String)
' Delimit keywords in unquoted part
Delimit = RE2.Replace(Unquoted, """$&""") & Quoted
End Function

WScript.Echo RE1.Replace(Str, GetRef("Delimit"))

--
Steve

When you enjoy loving your neighbor it ceases to be a virtue.
-Kahlil Gibran