A crude example

1. Senior Member
Join Date
Dec 1969
Posts
6,476

## A crude example

Looking for the most efficient way to do this.<BR>Regx using an array holding the info<BR>(0,0) regx<BR>(0,1) replace with<BR><BR>This is a crude example.<BR>Dim str2DArray(2,1)<BR>str2DArray(0,0) = "[[1]]" &#039;showing this for easy reading<BR>str2DArray(0,1) = "MyValue1"<BR>str2DArray(1,0) = "[[2]]"<BR>str2DArray(1,1) = "MyValue2"<BR>str2DArray(2,0) = "[[3]]"<BR>str2DArray(2,1) = "MyValue3"<BR><BR>Dim re<BR> For i = 0 to 2<BR> Set re = New RegExp &#039;<BR> strRegE = str2Darray(i,0)<BR> re.Pattern = strRegE <BR> re.Global = True<BR> re.IgnoreCase = True <BR> strMessage = re.Replace(strMessage,"\$1" & str2DArray(i,1) & "\$3")<BR>Next<BR><BR><BR>As you can see the regx searches individually for [[1]], [[2]], [[3]] and replaces with the values accordingly<BR><BR>Can you step through the message and for every [[ ]] get the value between, compare to the array, or a function and get what you need, replace, then move on to the next match?<BR>Basically going from start to finnish of the message only 1 time instead of looping...<BR><BR>Or is it best to loop through the array as i did and do them all 1 regx at a time?<BR><BR>Array would be about (40,1) so not small at all...<BR><BR>Thanks <BR>Russell M

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## I'm lost...

You&#039;ve already said<BR> re.Global = True<BR>so it&#039;s going to replace *EVERY* occurrence of the re with the noted replacement. No loop required for that.<BR><BR>So what is it you are asking???<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: I'm lost...

yes read it again and wondered if it would make sence to someone else.<BR><BR>Yes Individually it does replace whats in the array...<BR>Loop 1 Will replace all instances of [[1]] with myvalue1 in the message<BR>Then<BR>Loop 2 Will replace all instances of [[2]] with myvalue2 in the message<BR><BR>But in all I round be doing 40 loops through the array and doing 40 regx search & replaces...<BR><BR>Was wondering if it could be stepped through message 1 match at a time searching for an instance of whats between [[ ]] and replace...<BR><BR>Kinda like this.<BR><BR>Set re = New RegExp &#039; <BR> strRegE = str2Darray(i,0) <BR> re.Pattern = "(/[/[)(*?)(/]/])" <BR> re.Global = True <BR> re.IgnoreCase = True <BR>&#039;and step through the matches<BR>IF "\$2" = Something then<BR> strMessage = re.Replace(strMessage,"\$1" & str2DArray(i,1) & "\$3") <BR>Else <BR>END if<BR><BR>Very crude but might you see better what i ment?<BR>not sure how to step through it (if it can be done)<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## I'm not convinced...

...it would be any faster. Now you&#039;d be doing comparisons of that thing in the middle manually instead of letting the RE do it for you. Unless you only expected one or two of the possible 40 patterns per string, I&#039;d stick with what you have.<BR><BR>Yes, I think it can be done. You have to play with the MATCHES collection, I believe.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: I'm not convinced...

Yes there could be 0 matches or alot...<BR>a simular idea for understanding purposes would be like a custom formatting message board features...<BR>simular to what scott did here at 4 guys but just alot more items...<BR><BR>just go with that principal for now...<BR><BR>What you are saying is making sence...<BR>And im thinking maybe js regx might work better...<BR><BR> Dim objMatch<BR> Response.Write colMatches.Count & " matches found...&#060;P&#062;"<BR> &#039;Step through our matches<BR> For Each objMatch in colMatches<BR> <BR>Here is where the replace would go.<BR>so if i can something like<BR>Replace instr(message, objMatch.Value, (str2DArray(objMatch.Value,1))<BR> Next<BR> &#039;Clean up<BR> Set colMatches = Nothing<BR> Set objRegExpr = Nothing<BR><BR>Something like that<BR>Ill play with it a bit and see what i come up with...<BR><BR>Thanks for pointing me in right direction.<BR>Appreciate it.<BR><BR>Russell M

6. Senior Member
Join Date
Dec 1969
Posts
6,476

## This is real close

But not quite there yet<BR><BR> Dim objMatch<BR> &#039;this Steps through our matches<BR> For Each objMatch in colMatches<BR>Response.Write objMatch.Value & "<BR>"<BR>strSearchOn = Replace(strSearchOn, objMatch.Value, "MYTEST")<BR> Next<BR><BR><BR>Response.Write "<BR>" & strSearchOn<BR><BR>Ok so I know that works. (tested)<BR>Now how best to use the array...<BR>I would now need a 3d array<BR>0,0 hold regx "[[.*?]]"<BR>0,1 Hold true match (ie: [[ThisMatch]] )<BR>0,2 Hold replace value (ie: "value1" )<BR><BR> <BR>the key to this is matching objMatch.Value in the replace with the proper array elements...<BR><BR>Replace(strSearchOn, objMatch.Value, "MYTEST")<BR><BR>How best to find the match in the array where<BR>0,1 Hold true match (ie: [[ThisMatch]] ) = objMatch.Value<BR><BR>[im no JS guy] <BR>but maybe this task will be easier to do in JS?<BR>I think you can just use a nameing convention in JS Arrays?<BR><BR>Dim jsArray<BR>jsArray("[[something]]") = "MYTEST1" &#039;my replacement value<BR><BR>And inside my Loop of matches do a replace something like this<BR> <BR>For Each objMatch in colMatches<BR> Replace(strSearchOn, objMatch.Value, jsArray(objMatch.Value))Next<BR><BR>Js or Vbs?<BR>Any ideas how best to work with this scheme?<BR><BR>I might go to bed shortly so will need to pick it up in the morning...<BR><BR>Eyes are starting to go buggy.<BR><BR>Thanks again for any ideas...<BR>Russell M

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## I don't get it...

I thought the whole idea of this was to use the same RegExp for all cases.<BR><BR>So why do you need<BR> 0,0 hold regx "[[.*?]]"<BR> 0,1 Hold true match (ie: [[ThisMatch]] )<BR> 0,2 Hold replace value (ie: "value1" )<BR>when that regx is going to be the same throughout???<BR><BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
6,476

## That was a typo.

Was tired and copy/pasted it over.<BR><BR>I only used the 1 regx<BR><BR>objRegExpr.Pattern = "[[.*?]]"<BR> objRegExpr.Global = True<BR> objRegExpr.IgnoreCase = True<BR>Dim colMatches, objMatch <BR> Set colMatches = objRegExpr.Execute(strSearchOn)<BR><BR> &#039;this Steps through our matches <BR> For Each objMatch in colMatches <BR>strSearchOn = Replace(strSearchOn, objMatch.Value, "MYTEST") <BR> Next <BR><BR>Response.Write "&#060;/br&#062;" & strSearchOn &#039;just to see if it works<BR><BR>So this will find and change all the matches.<BR>(so dont need to store the regx in the array..)<BR>but still need a 2d array to hold a Match & replace<BR> <BR><BR>So lets look at our first match in the collection<BR>ie: objMatch.Value = [[ThisMatch]]<BR><BR>How do i find it and it&#039;s corrisponding value from the array?<BR>0,0 = "[[ThisMatch]]" <BR>0,1 = "value1" <BR>1,0 = "[[ThisMatch2]]" <BR>1,1 = "value2"<BR>0,0 = "[[ThisMatch3]]" <BR>0,1 = "value3" <BR> <BR>So basically match objMatch.Value with (0,0) but use its value in array of (0,1) in my replacement?<BR><BR>Replace(strSearchOn, objMatch.Value, MyArray(0,1)) <BR><BR>Looking at an article on Filtering arrays which seems can only be done on 1d arrays.<BR>(But might less efficient than looping through my full array with all 40 regx&#039;s in there)<BR><BR>I was oncall today & have to go into the office.<BR>Will check back here later this aft, and do more searching.<BR><BR>Thanks again<BR>Russell M

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•