Vbscript 从文本行中获取第一个、第二个和第三个数字

Vbscript 从文本行中获取第一个、第二个和第三个数字,vbscript,Vbscript,我的txt文件中有以下格式: 1 1,30705856804525E-7 2,64163961816166E-8 1,12201845645905 1,24157281788939E-7 2,45690063849224E-8 1,25892543792725 1,18248813407718E-7 2,29960868125545E-8 1,41253757476807 1,13006606738963E-7 2,16654658657944E-8 1,584893226

我的txt文件中有以下格式:

1 1,30705856804525E-7 2,64163961816166E-8 1,12201845645905 1,24157281788939E-7 2,45690063849224E-8 1,25892543792725 1,18248813407718E-7 2,29960868125545E-8 1,41253757476807 1,13006606738963E-7 2,16654658657944E-8 1,58489322662354 1,0842624220686E-7 2,05472137082552E-8 1,77827942371368 1,04479198625995E-7 1,96135836461053E-8 1,99526226520538 1,01119816520168E-7 1,8839035220708E-8 2,23872113227844 9,82917924829962E-8 1,82003176973922E-8 2,51188635826111 9,59338279926669E-8 1,76765304615856E-8 2,81838297843933 9,39840489877497E-8 1,72491425587395E-8 3,16227769851685 9,23831819932275E-8 1,69019571671924E-8 3,54813385009766 9,10766573269939E-8 1,66210121221866E-8 3,98107171058655 9,00157104410937E-8 1,63944182673958E-8 4,46683597564697 8,91577514039454E-8 1,62121711611007E-8 5,01187229156494 8,8466336478632E-8 1,60659361370108E-8 5,6234130859375 8,7910699164695E-8 1,59488209305891E-8 6,30957365036011 8,74651959748007E-8 1,58551749507296E-8 7,07945775985718 8,71086527354237E-8 1,57803938805046E-8 7,94328212738037 8,68237393092386E-8 1,57207402651238E-8 8,91250896453857 8,65963372120859E-8 1,56731942979604E-8 10 8,64150138113473E-8 1,56353241465013E-8 11,2201843261719 8,62705391568852E-8 1,5605175818223E-8 130705856804525E-7 264163961816166E-8 112201845645905 1241572821788939E-7 245690063849224E-8 125892543792725 118248813407718E-7 229960868125545E-8 141253757476807 113006606738963E-7 216654657944E-8 15848932262354 10842624220686E-7 205472137082552E-8 177827942371368 104479198625995E-7 196135836461053E-8 199526226520538 101119816520168E-7 18839035220708E-8 2238721132278449827924829962E-8 182003176973922E-8 251188635826111 959338279926669E-8 176765304615856E-8 281838297843933 939840489877497E-8 1724941425587395E-8 316227769851685923831819932275E-8 169019571671924E-8 35481338509766 910766573269939E-8 166210121221866E-8 398107171058655 900157104410937E-8 163944182673958E-8 446683597564697 891577514039454E-8 162121711611007E-8 5011872291564948466336478632E-8 160659361370108E-8 56234130859375 87910699164695E-8 159488209305891E-8 63095735036011 874651959748007E-8 158551749507296E-8 707945775985718 871086527354237E-8 15780938805046E-8 794328212738037 868237393092386E-8 157207402651238E-8 89125089645377 865963372120859E-8 156731942979604E-8 10 864150138113473E-8 156353241465013E-8 112201843261719 862705391568852E-8 1565175818223E-8 我只需要得到左整数和右整数的值,所以在本例中,我需要得到:

1 2,64163961816166E-8 10 1,56353241465013E-8 1. 264163961816166E-8 10 15635241465013E-8 这就是我尝试过的:

' Check Noise Spectral Density.txt exists
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(fso.GetParentFolderName(WScript.ScriptFullName) + "\Projects\Noise Spectral Density.txt")) Then
    ' Open the file for input.
    Set NoiseSpectralDensity = fso.OpenTextFile(fso.GetParentFolderName(WScript.ScriptFullName) + "\Projects\Noise Spectral Density.txt", 1)

    ' Noise Variables
    Dim Noise

    ' Read from the file and display the results.
    Do While NoiseSpectralDensity.AtEndOfStream <> True
        ' Read Line By Line
        TextLine = NoiseSpectralDensity.ReadLine

        ' Check If Number
        'If (IsNumeric(Left(TextLine, 5))) Then
            ' Get Noise

            ' Noise @ 1kHz
            Noise = Right(TextLine, InStr(Mid(TextLine, 2), InStr(TextLine, " ")-1))
            x = MsgBox("SNR: " & Split(Left(TextLine, 5), " ")(0) & " NOISE: " & Noise & "",0, "Noise Spectral Density")
        'End If
    Loop

    ' Close the file for input.
    NoiseSpectralDensity.Close
Else
    x = MsgBox("Noise Spectral Density.txt NOT Found!" & vbCrLf & "Wave->Save As Text...", 0, "Noise Spectral Density")
End If
“检查噪声频谱密度.txt是否存在
设置fso=CreateObject(“Scripting.FileSystemObject”)
如果(fso.FileExists)(fso.GetParentFolderName(WScript.ScriptFullName)+“\Projects\Noise spectrum Density.txt”)),那么
'打开文件进行输入。
设置NoiseSpectralDensity=fso.OpenTextFile(fso.GetParentFolderName(WScript.ScriptFullName)+“\Projects\Noise spectraldensity.txt”,1)
“噪声变量
微弱噪音
'读取文件并显示结果。
请勿随意喧哗,特别是。AtEndOfStream True
“逐行阅读
TextLine=NoiseSpectralDensity.ReadLine
“检查一下号码
'如果是数字(左(文本行,5)),则
“有声音吗
“1kHz时的噪声
噪声=右侧(文本行,仪表(中间(文本行,2),仪表(文本行,“”)-1))
x=MsgBox(“信噪比:&Split(左(文本行,5),”)(0)和“噪声:&NOISE&”,0,“噪声谱密度”)
"完"
环
'关闭文件以进行输入。
噪音特别大,关上
其他的
x=MsgBox(“找不到噪波频谱密度.txt!”&vbCrLf&“波形->另存为文本…”,0,“噪波频谱密度”)
如果结束

但是我无法在VBScript中使用
Split(TextLine,”)(0)
获取左右数字。您的数据似乎是用制表符分隔的,因此可以执行以下操作:

arr = Split(TextLine, vbTab)
If Not (InStr(arr(0), ",") > 0) Then
    'first number doesn't have decimals
    snr   = arr(0)
    noise = arr(2)
End If

尽管@AnsgarWiechers提供的解决方案应该可以工作,但如果不行,您可以使用正则表达式(将整个Do-while循环替换为以下内容):

Set objReg=New RegExp
objReg.Pattern=“^(\d+(=\s)。*\s+([\d,Ee-]+)$”请参见下面的说明
请勿随意喧哗,特别是。AtEndOfStream True
“逐行阅读
TextLine=NoiseSpectralDensity.ReadLine
“检查一下号码
Set objMatches=objReg.Execute(文本行)
对于objMatch中的每个objMatch
SNR=对象匹配子匹配项(0)
噪声=对象匹配。子匹配。项目(1)
MsgBox“信噪比:&SNR&”噪声:&Noise
下一个
环

正则表达式解释:

  • ^
    -断言字符串的开头
  • (\d+)
    -匹配数字的1+次出现次数,并将其捕获到组1中
  • (?=\s)
    -正向向前看,以查找前面紧跟空白的位置。因此,在遇到空白(空格、制表符等)之前,将匹配步骤2中的数字
  • *
    -匹配除换行符以外的任何字符的0+次出现次数
  • \s+
    -匹配1+次出现的空白
  • ([\d,Ee-]+)
    -匹配1+个数字或
    -
    或字母
    E
    E
    的出现次数,并将其捕获到组2中
  • $
    -断言字符串的结尾

我会设置
noise=arr(UBound(arr))
以防一行中有三个以上的数字。@AhmedAbdelhameed我们对数据导出的了解不够,无法做出这样的假设。如果有更多的列,它们很可能包含额外的数据,而OP想要提取的数据保留在第三列。是的,我没有仔细阅读标题。然而,根据“我只需要得到左值和右值上整数的值”的说法,这似乎是OP真正想要的。不过,考虑到标题,你的是最安全的:)
Set objReg = New RegExp
objReg.Pattern = "^(\d+)(?=\s).*\s+([\d,Ee-]+)$"     'See the explanation below
Do While NoiseSpectralDensity.AtEndOfStream <> True
    'Read Line By Line
    TextLine = NoiseSpectralDensity.ReadLine

    ' Check If Number
    Set objMatches = objReg.Execute(TextLine)
    For Each objMatch In objMatches
        SNR = objMatch.submatches.item(0)
        Noise = objMatch.submatches.item(1)
        MsgBox "SNR: "&SNR&"; Noise: "&Noise
    Next
Loop