Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Multiple Match per string not working

Status
Not open for further replies.

brettz

Programmer
Jul 18, 2002
42
US
I'm trying to make a RegExp match for any text that is in between '<<<' and '>>>' However, I am unable to do this unless there is a carriage return within the string being evaluated by the Regular Expression object. For example, if I evaluate the following string, I will only get a match on the first instance of text between '<<<' and '>>>' per line:

Type: <<<Type>>>
If Type = &quot;Professional Liability&quot; then enter <<<Occurance>>> and <<<Aggregate>>>

I get a match for 'Type' and 'Occurance', but NOT for 'Aggregate'

Here is the code I am using:

'Create Regualar Expression Object
Set objRE = New RegExp
'objRE.Pattern = &quot;&#60;&#60;&#60;[\s\S].[^>]*&#62;&#62;&#62;&quot; 'to match <<< >>> tags and contents
objRE.Global = True
'Get all matches for any text within special character Set objMatches = objRE.Execute(description)

I can't figure out why I only get one match per line. I have spent hours trying to figure this out and have researched several web sites regarding RegExp thorougly.

Please advise...I would greatly appreciate any help or ideas.

Thanks,

-Brett
 
You need to use a For...Each expression.

Go to Microsoft downloads and get the WSH (Windows Scripting Host) documentation. There's a very nifty example in there.

Lemme see if I can find it...

Code:
Function SubMatchTest(inpStr)
  Dim oRe, oMatch, oMatches
  Set oRe = New RegExp
  ' Look for an e-mail address (not a perfect RegExp)
  oRe.Pattern = &quot;(\w+)@(\w+)\.(\w+)&quot;
  ' Get the Matches collection
  Set oMatches = oRe.Execute(inpStr)
  ' Get the first item in the Matches collection
  Set oMatch = oMatches(0)
  ' Create the results string.
  ' The Match object is the entire match - dragon@xyzzy.com
  retStr = &quot;Email address is: &quot; & oMatch & vbNewline
  ' Get the sub-matched parts of the address.
  retStr = retStr & &quot;Email alias is: &quot; & oMatch.SubMatches(0)  ' dragon
  retStr = retStr & vbNewline
  retStr = retStr & &quot;Organization is: &quot; & oMatch. SubMatches(1)' xyzzy
  SubMatchTest = retStr
End Function

MsgBox(SubMatchTest(&quot;Please send mail to dragon@xyzzy.com. Thanks!&quot;))

And then there's

Code:
Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches   ' Create variable.
   Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = patrn   ' Set pattern.
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.
   Set Matches = regEx.Execute(strng)   ' Execute search.
   For Each Match in Matches   ' Iterate Matches collection.
      RetStr = RetStr & &quot;Match found at position &quot;
      RetStr = RetStr & Match.FirstIndex & &quot;. Match Value is '&quot;
      RetStr = RetStr & Match.Value & &quot;'.&quot; & vbCRLF
   Next
   RegExpTest = RetStr
End Function
MsgBox(RegExpTest(&quot;is.&quot;, &quot;IS1 is2 IS3 is4&quot;))

Play with those and I think you'll get your answer.

Onwards,

Q-
 

use a pattern like &quot;<<<([\s\S]+)>>>&quot;
and then check out Match.subMatches(0) for the contents of ( ).

Look here for a good explanation:
thread329-616385




Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
I am getting more than one match per description/string being passed to the RegExp object. However, I am not getting more than one match per line. I only get one match until there is a hard return in my string. After there is a hard return, I will get only one more match until there is another hard return in the string under consideration.

I should have included my For/Next Loop that I am using in my code. Here is the complete code:

Code:
'Create Regualar Expression Object to separate Parameters in order
Set objRE = New RegExp
objRE.Pattern = &quot;&#60;&#60;&#60;[\s\S].[^>]*&#62;&#62;&#62;&quot; 'matches all <<< >>> tags and contents
objRE.Global = True
'Get all matches for any text within special character ASCII &lt, &gt
Set objMatches = objRE.Execute(description)
If objMatches.Count > 0 Then
'Replace Reqular Expression &quot;<<<abc>>>&quot; with Input text boxes
For Each objMatch In objMatches
	ParameterNames(1,4) = objMatch.Value
Next

The funny thing is that if I put the ASCII equivalent for the '>' in the following line of code:
Code:
objRE.Pattern = &quot;&#60;&#60;&#60;[\s\S].[^&#62;]*&#62;&#62;&#62;&quot;

Then, I will get all of the matches on one line of the string...in the initial example from my first post above that means I will get a match for 'Type' and 'Occurance'AND 'Aggregate' However, the problem is that it won't match anything having '&' '#' '6' '2' or ';' I tried using the various '\' escape values as recommended at url=/library/en-us/script56/html/jsgrpRegExpSyntax.asp ('\xn' '\n', etc.), however this did not work either.

Any other suggestions would be greatly appreciated.

Thanks,

-Brett
 
Well, I have it working by using a non ASCII character, which means I have to use something on the keyboard. I don't like this though and would prefer a solution where I can use ASCII, dec, hex, or oct, so that the code cannot be broken if a user decides to use the '`' on the keyboard. I chose to use the '`' b/c it is probably the lease likely to be used. However, if someone happens to use this in the string outside of the parameter enclosed in '`' then the code will be broken!

I tried the following:
&#60;&#60;&#60;[\s\S].[^\x3E]*&#62;&#62;&#62;
However, I was still unable to capture more than one match per line in my string, although this did still find matches with 'x' '3' and 'E'. I don't understand why these hex, dec, ASCII characters aren't working as they are supposed to?

Any guru who has an answer will be dubbed guru master in my book :)

Thanks for all of the attempts to help.

-Brett
 
[tt]
Dim myMatches
Dim myMatch

With New RegExp
.Global = True
.MultiLine = True
.Pattern = &quot;<<<.+?>>>&quot;
Set myMatches = .Execute(Description)
End With

For Each myMatch In myMatches
MsgBox myMatch
Next
 
OK, went ahead and actually did it:
Code:
     Dim RegX, Match, matchesColl, sPattern, sReplaceWith, sString
    
    sPattern = &quot;<<<([\s\S]+?)>>>&quot;
    sString = &quot;Type: <<<Type>>>&quot; & vbCrLF & &quot;If Type <<<Occurance>>> and <<<Aggregate>>>&quot;

    Set RegX = NEW RegExp
    RegX.Pattern = sPattern
    RegX.Global = True
    RegX.IgnoreCase = True
    RegX.MultiLine = True
    
    Set matchesColl = RegX.Execute(sString)
    
    Response.write(&quot;Searching: &quot; & Server.HTMLEncode(sString) & &quot;<br>&quot;)
    For Each Match in matchesColl
      Response.Write(&quot;Match: &quot; & Server.HTMLEncode(Match) & &quot;, Match.subMatches(0): &quot; & Match.subMatches(0) & &quot;<br>&quot;)
    Next

server.HTMLEncode() is cos i'm using ASP and otherwise printing < and > get's messed up. The above gives me:

[tt]Searching: Type: <<<Type>>> If Type <<<Occurance>>> and <<<Aggregate>>>
Match: <<<Type>>>, Match.subMatches(0): Type
Match: <<<Occurance>>>, Match.subMatches(0): Occurance
Match: <<<Aggregate>>>, Match.subMatches(0): Aggregate
[/tt]

Now, what do you mean about special chars?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
strongm

When I try <code>.Pattern = &quot;<<<.+?>>>&quot;</code>
I get the following error:

Microsoft VBScript runtime (0x800A139A)
Unexpected quantifier

I will try clarkin's suggestion.

Thanks,

-Brett
 
Well,

I'm getting the same error for clarkin's suggestion:

Microsoft VBScript runtime (0x800A139A)
Unexpected quantifier

I tested the code and it is the '?' that is causing the problem.

Also, I am getting the following error for the .MultiLine property of the Regular Expression Object.

Object doesn't support this property or method: 'MultiLine'

Maybe you guys are using .NET or something, which supports all of the VB methods and properties. I am not using .NET.

Any other ideas would be greatly appreciated.

Thanks,

-Brett
 
You're probably using an older version of vbscript and need to update your pws or IIS :)

Run this:
Code:
Dim s
   s = &quot;&quot;   ' Build string with necessary info.
   s = ScriptEngine & &quot; Version &quot;
   s = s & ScriptEngineMajorVersion & &quot;.&quot;
   s = s & ScriptEngineMinorVersion & &quot;.&quot;
   s = s & ScriptEngineBuildVersion 
   response.write s

The question mark in the pattern is to make the previous wildcard non-greedy. Basically stops the match at the first >>> instead of eating up the whole two lines to the last one.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Yep, it's beginning to look like you are still using Microsoft Scripting 5.0, which does not have the Multiline property, and uses ? differently from later versions
 
Apparently, I am using VBScript Version 5.1.7426.
What versions should I be using to run the examples you all have been giving me? Where can I download this? And will it require a reboot?

Thanks,

-Brett
 
The funny thing is that it will get multiple matches per line if I use a different delimiter, such as a tilde or the pound sign? It just won't match when I use the greater than or less than signs.

I will try upgrading to VBScript 6.0, but this may take some time to get through the red tape here at work.

Thanks,

-Brett
 
To have a decently recent VBS runtime, simply upgrade your Internet Explorer to IE6.0 SP1

Hope This Help
PH.
 
Unfortunately, we cannot upgrade to IE 6.0 because many of our applications cannot run off of anything later than IE 5.5. Is there any other way to get just the VBScript upgrade?

If not, then does any one know how I can find out more about the Reg Expressions characters that work with VBScript Version 5? The MS web site regarding RegExp, doesn't designate which version is under consideration for the characters listed at the web site?

Thanks,

-Brett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top