在 VB 2010 中使用它:
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 lines() As String = IO.File.ReadAllLines("filename.txt")
Dim foundIndex As Integer = Array.FindIndex(lines, Function(l) l.StartsWith(TextBox1.Text)) 'assuming TextBox1.Text = the part of the line before the =
lines(foundIndex) = Regex.Replace(lines(foundIndex), "(?<=" & TextBox1.Text & "=)\d+", TextBox2.Text) 'TextBox2.Text is the replacement number
Stop
'to rewrite the file:
'IO.File.WriteAllLines("filename.txt", lines)
End Sub
End Class
...但是每次我保存文本文件时,它都会在等号后添加数字。
我希望它在等号之后替换该行上的所有内容。