Vbscript在txt文件中搜索单词并仅返回单词 [英] Vbscript search for word in a txt file and return only the word

查看:80
本文介绍了Vbscript在txt文件中搜索单词并仅返回单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天。我是vbscripting&的初学者需要一些帮助才能使我的代码正确。我有一个代码,用于搜索具有89的文本文件中的单词,并且只返回完整的单词。我的代码目前返回整行/句子。



代码>>>



< br $>
Const ForReading = 1



设置objRegEx = CreateObject(VBScript.RegExp)

objRegEx.Pattern = 89



设置objFSO = CreateObject(Scripting.FileSystemObject)



设置objFile = objFSO .OpenTextFile(C:\ Temp \ test.txt,ForReading)





Do until objFile.AtEndOfStream



strSearchString = objFile.ReadLine





设置colMatches = objRegEx.Execute(strSearchString )



如果colMatches.Count> 0然后



每个strMatch in colMatches



Wscript.Echo strSearchString



下一页



结束如果



循环





objFile.Close





<< <代码





test.txt文件>>





接口Cellular的准备信息:

----------------------------- --------

状态:准备开机并注册

紧急模式:关闭

订阅者ID:655020007620408

SIM ICC Id:89270200150007090909

电话号码:0



我的尝试:



Good day. I am beginner to vbscripting & need some assistance in getting my code right. I have got a code that search for word in a text file that has "89" and got to return only the full word. My code currently returns the whole line/sentence.

code >>>


Const ForReading = 1

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "89"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Temp\test.txt", ForReading)


Do Until objFile.AtEndOfStream

strSearchString = objFile.ReadLine


Set colMatches = objRegEx.Execute(strSearchString)

If colMatches.Count > 0 Then

For Each strMatch in colMatches

Wscript.Echo strSearchString

Next

End If

Loop


objFile.Close


<<< code


test.txt file >>


Ready information for interface Cellular:
-------------------------------------
State : Ready to power up and register
Emergency mode : Off
Subscriber Id : 655020007620408
SIM ICC Id : 89270200150007090909
Number of telephone numbers : 0

What I have tried:

expected output > 89270200150007090909

currently output > SIM ICC Id : 89270200150007090909

推荐答案

这不仅是一个VbScript问题,也是一个正则表达式问题。



第一个错误是你的脚本中的一个错误:

This is not only a VbScript problem but also a regex problem.

The first error is a mistake in your script:
For Each strMatch in colMatches
    Wscript.Echo strSearchString
Next

您正在打印 strSearchString (从文件中读取的行)而不是 strMatch (每场比赛)。



要获得匹配的部分,请用括号括起来。但是,这需要包括以下字符,直到出现单词分隔字符。在您的情况下,您可以简单地匹配89后跟一个或多个数字:

You are printing strSearchString (the line read from the file) instead of strMatch (each match).

To get only the matched parts, enclose them with parentheses. But that requires to include the following characters until a word separation character occurs. In your case you can simply match for "89" followed by one or more digits:

objRegEx.Pattern = "(89\d+)"

如果你知道总长度总是20,你也可以指定必须有18个以下数字:

If you know that the total length is always 20, you can also specify that there must be 18 following digits:

objRegEx.Pattern = "(89\d{18})"



参见常规表达式语法(脚本) [ ^ ]。


这篇关于Vbscript在txt文件中搜索单词并仅返回单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆