VB.NET读取文本文件中的某些文本 [英] VB.NET Read Certain text in a text file
问题描述
acc = blah
pass = hello
我希望我的vb.net应用程序能够知道帐户变量等于blah,密码变量等于你好。
任何人都可以告诉我如何做到这一点?
谢谢
$这是一小段代码,点击按钮后,
- 获取一个输入文件(在这种情况下,我创建了一个名为test.ini的文件)
-
- 使用正则表达式搜索它是否包含任何ACC =或PASS =参数
- 然后将它们写入控制台
$ bImports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal send呃作为System.Object,ByVal e As System.EventArgs)处理Button1.Click
Dim strFile As String =Test.INI
Dim sr As New StreamReader(strFile)
Dim InputString As字符串
虽然sr.Peek<> -1
InputString = sr.ReadLine()
checkIfContains(InputString)
InputString = String.Empty
End while
sr.Close()
End Sub
Private Sub checkIfContains(ByVal inputString As String)
Dim outputFile As String =testOutput.txt
Dim m As Match
Dim m2 As Match
Dim itemPattern As String =acc =(\S +)
Dim itemPattern2 As String =pass =(\S +)
$ bm = Regex.Match(inputString,itemPattern ,_
RegexOptions.IgnoreCase或RegexOptions.Compiled)
m2 = Regex.Match(inputString,itemPattern2,_
RegexOptions.IgnoreCase或RegexOptions.Compiled)
Do While m.Success
Console.WriteLine(Found account {0},_
m.Groups(1),m.Groups(1).Index)
m = m.NextMatch()
Loop
Do while m2.Success
Console.WriteLine(Found password {0},_
m2.Groups(1),m2.Groups(1).Index)
m2 = m2.NextMatch()
Loop
End Sub
End Class
I want my program to read certain text in a text file. For example if I have a text file that contains the following info..
acc=blah pass=hello
I want my vb.net application to get that the account variable is equal to blah, and the password variable is equal to hello.
Can anyone tell me how to do this?
Thanks
解决方案Here is a quick little bit of code that, after you click a button, will:
- take an input file (in this case I created one called "test.ini")
- read in the values as separate lines
- do a search, using regular expressions, to see if it contains any "ACC=" or "PASS=" parameters
- then write them to the console
here is the code:
Imports System.IO Imports System.Text.RegularExpressions Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strFile As String = "Test.INI" Dim sr As New StreamReader(strFile) Dim InputString As String While sr.Peek <> -1 InputString = sr.ReadLine() checkIfContains(InputString) InputString = String.Empty End While sr.Close() End Sub Private Sub checkIfContains(ByVal inputString As String) Dim outputFile As String = "testOutput.txt" Dim m As Match Dim m2 As Match Dim itemPattern As String = "acc=(\S+)" Dim itemPattern2 As String = "pass=(\S+)" m = Regex.Match(inputString, itemPattern, _ RegexOptions.IgnoreCase Or RegexOptions.Compiled) m2 = Regex.Match(inputString, itemPattern2, _ RegexOptions.IgnoreCase Or RegexOptions.Compiled) Do While m.Success Console.WriteLine("Found account {0}", _ m.Groups(1), m.Groups(1).Index) m = m.NextMatch() Loop Do While m2.Success Console.WriteLine("Found password {0}", _ m2.Groups(1), m2.Groups(1).Index) m2 = m2.NextMatch() Loop End Sub End Class
这篇关于VB.NET读取文本文件中的某些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!