问题
I have a file with the following text as a result of my other vbscript:
              "Name": "stopped"
            "LaunchTime": "2015-02-13<some-text>", 
            "InstanceId": "i-<something>", 
                "Name": "stopped"
            "LaunchTime": "2015-02-13T17:24:11.000Z", 
            "InstanceId": "i-<something>", 
                "Name": "stopped"
            "LaunchTime": "2015-02-12<some-text>", 
            "InstanceId": "i-<something>", 
I want to use 4 characters as delimiter -> ": " (which is 4 characters) and display everything at the right side of it, so the result will look like this:
"stopped"
"2015-02-13<some-text>", 
"i-<something>", 
"stopped"
"2015-02-13T17:24:11.000Z", 
"i-<something>", 
 "stopped"
"2015-02-12<some-text>", 
"i-<something>", 
How can I accomplish such thing using vbscript? It seems that in bash, perl, awk and even linux command line things like that are so simple. Is there an easy way to do it? I was looking into split but I can't figure it out how to do it.
Thank you
回答1:
The simplest way would be to Split the lines at : (a colon followed by a space) and display the second field (index 1) of the resulting array:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\path\to\your.txt")
Do Until f.AtEndOfStream
  line = f.ReadLine
  If InStr(line, ": ") > 0 Then WScript.Echo Split(line, ": ", 2)(1)
Loop
f.Close
回答2:
Or, if you want a RegExp, extract the data after the ": ". In code:
Option Explicit
Dim s : s = Replace(Join(Array( _
 "            'Name': 'stopped'                       ", _
 "          'LaunchTime': '2015-02-13<some-text>',    ", _
 "          'InstanceId': 'i-<something>',            ", _
 "              'Name': 'stopped'                     ", _
 "          'LaunchTime': '2015-02-13T17:24:11.000Z', ", _
 "          'InstanceId': 'i-<something>',            ", _
 "              'Name': 'stopped'                     ", _
 "          'LaunchTime': '2015-02-12<some-text>',    ", _
 "          'InstanceId': 'i-<something>',            "  _
), vbCrLf), "'", """")
WScript.Echo s
WScript.Echo "-----------"
Dim r : Set r = New RegExp
r.Global  = True
r.Pattern = ": (""[^""]+"",?)"
Dim ms : Set ms = r.Execute(s)
Dim i
For i = 1 To ms.Count
    WScript.Echo ms(i - 1).SubMatches(0)
    If 0 = i Mod 3 Then WScript.Echo
Next
output:
cscript 28751963.vbs
            "Name": "stopped"
          "LaunchTime": "2015-02-13<some-text>",
          "InstanceId": "i-<something>",
              "Name": "stopped"
          "LaunchTime": "2015-02-13T17:24:11.000Z",
          "InstanceId": "i-<something>",
              "Name": "stopped"
          "LaunchTime": "2015-02-12<some-text>",
          "InstanceId": "i-<something>",
-----------
"stopped"
"2015-02-13<some-text>",
"i-<something>",
"stopped"
"2015-02-13T17:24:11.000Z",
"i-<something>",
"stopped"
"2015-02-12<some-text>",
"i-<something>",
来源:https://stackoverflow.com/questions/28751963/how-to-display-text-after-pattern-in-vbscript