Question

I am writing a vbscript file to parse data out of a log file. Log file has this structure in it, always formatted this certain way:

<name="ExecResponse" value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX==" />

How can I just match the data in between the quotes (XXXXX), even with 0 or more new lines? Not language specific, but I am validating in Textpad, so not sure if global operators are available to me, but in VBScript they are.

Thanks.

Was it helpful?

Solution 2

VBScript solution, since you tagged your question :

Set fso = CreateObject("Scripting.FileSystemObject")
txt = fso.OpenTextFile("C:\path\to\your.log").ReadAll

Set re = New RegExp
re.Pattern = """([^""]*)"""
re.Global = True

For Each m In re.Execute(txt)
  WScript.Echo m.SubMatches(0)
Next

Demonstration:

>>> s = "<name=""ExecResponse"" value=""XXXXXXXXXXXXXXXXXXXXXXX" & vbNewLine & _
"XXXXXXXXXXXXXXXXXXXXXXX" & vbNewLine & _
"XXXXXXXXXXXXXXXXXXXXXXX" & vbNewLine & _
"XXXXXXXXXXXXXXXXXXXXXXX" & vbNewLine & _
"XXXXXXXXXXXXX=="" />"
>>> WScript.Echo s
<name="ExecResponse" value="XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX==" />
>>> Set re = New RegExp
>>> re.Pattern = """([^""]*)"""
>>> re.Global = True
>>> For Each m In re.Execute(s) : WScript.Echo m.SubMatches(0) : Next
ExecResponse
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX==

The actual regular expression is "([^"]*)", but the double quotes must be doubled to escape them inside the string.

If you want a more specific match (e.g. just the value of the value attribute), you need to make the regular expression more specific, e.g. like this: value="([^"]*)".

OTHER TIPS

The easiest way would be to use /"[^"]*"/g, assuming that all quotes are correctly balanced, and none are escaped.

Something like this:

value\="([^"]*)"

Or this if you want to allow possible spaces:

value[[:space:]]?\=[[:space:]]?"([^"]*)"

In theory, the word value followed by an escaped equals sign followed by a quote, followed by anything that's not a quote, followed by another quote.

I'm not familiar with VB script but the 'anything but a quote' part should also include new lines. Note in other languages there are switches to include new lines.

Ex PHP uses the /s modifier for new lines:

<?php
preg_match('/value\="([^"]*)"/s',$string);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top