tags:

views:

27

answers:

3

I am using vbscript regex to find self-defined tags within a file.

"\[\$[\s,\S]*\$\]"

Unfortunately, I am doing something wrong, so it will grab all of the text between two different tags. I know this is caused by not excluding "$]" between the pre and post tag, but I can't seem to find the right way to fix this. For example:

[$String1$]
useless text
[$String2$]

returns

[$String1$]
useless text
[$String2$]

as one match.

I want to get

[$String1$]
[$String2$]

as two different matches.

Any help is appreciated.

Wade

+1  A: 

Make the * quantifier lazy by adding a ?:

"\[\$[\s\S]*?\$\]"

should work.

Or restrict what you allow to be matches between your delimiters:

"\[\$.*\$\]"

will work as long as there is only one [$String$] section per line, and sections never span multiple lines;

"\[\$(?:(?!\$\])[\s\S])*\$\]"

checks before matching each character after a [$ that no $] follows there.

Tim Pietzcker
+1 I found a solution that worked better for me, but this was helpful in learning a bit more about regular expressions.
Wade73
+1  A: 

The RegEx is greedy and will try to match as much as it can in one go.

For this kind of matching where you have a specific format, instead of matching everything until the closing tag, try matching NOT CLOSING TAG until closing tag. This will prevent the match from jumping to the end.

"\[\$[^\$]*\$\]"
Erik Noren
+1 That's it, worked like a charm and it works over multiple lines.
Wade73
+1  A: 

No need to use regex. try this. If your tags are always defined by [$...$]

Set objFS = CreateObject( "Scripting.FileSystemObject" )
strFile=WScript.Arguments(0)
Set objFile = objFS.OpenTextFile(strFile)
strContent = objFile.ReadAll
strContent = Split(strContent,"$]")
For i=LBound(strContent) To UBound(strContent)
    m = InStr( strContent(i) , "[$" )
    If  m > 0 Then
        WScript.Echo Mid(strContent(i),m) & "$]"
    End If 
Next
ghostdog74
Works, but I am going to stick with the regex solution. Thanks.
Wade73