Vb.net:正则表达式查找和替换 [英] Vb.net: regex find and replace

查看:338
本文介绍了Vb.net:正则表达式查找和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts!

i我做了一个案例,用户可以通过选择多个字符串来创建自定义公式,



现在,当用户保存公式,就是这种形式



Hello Experts !
i am making a case in which user can create custom formula by selecting multiple strings,

Now, when user saves the formula, it is in this form

引用:

(AL01- RM)+(AL03-RM)+20.0

(AL01-RM)+(AL03-RM)+20.0





括号内的字符串是元素的一些ID(在datagridview列中),

现在的情况是,



1-它得到上面的字符串,

2-找到括号内的字符串()在datagridview列中由Regex模式(?< = \()。*?(?= \)))

3-如果字符串内部为bracket()与datagridview第1列值匹配,

4-现在从datagridview第2列获取值(即字符串的值)

5-现在替换括号()内第一个输入字符串中的值,并且这里是我卡住了,

i想要这样的字符串

(10)+(30)+20



但我得到了字符串喜欢这个

(10)+(AL03-RM)+20.0(AL01-RM)+(30)+20.0



i我尝试了不同的东西,但是不能出来就像

(10)+(30)+20

详情请见图片



Imgur:图片链接了解详情 [ ^ ]



请帮我解决这个问题。 />


我尝试过:



Private Sub SimpleButton2_Click(sender As Object, e作为EventArgs)处理SimpleButton2.Click



对于i as Integer = 0到dgv.Rows.Count - 1



'Dim paren As New Regex(\([^]] * \))

'Dim paren As New Regex(\((。*?)\\ \\))

Dim paren As New Regex((?< = \()。*?(?= \)))

Dim命令As String =((AL01-RM)+(AL03-RM))+ 20.0



Dim匹配为MatchCollection

匹配= paren.Matches(命令)



Dim m As Match

每个m在比赛中



如果m.ToString = dgv.Rows(i).Cells(1).Value那么

RTB2.AppendText(Regex.Replace(command,m.ToString,dgv.Rows(i).Cells(2).Value.ToString))



End如果

下一页



下一页



结束Sub



inside brackets Strings are some ids of the elements (that are in datagridview column),
now the scenario is,

1- it gets the above string,
2- find string inside brackets () in datagridview column by Regex pattern "(?<=\().*?(?=\))")
3- if string insides brackets() matches with datagridview column 1 value,
4- now get the value from datagridview column 2 (that is the value of string)
5- now replace value in first input string inside brackets() , and here is i have stuck,
i want string like this
"(10)+(30)+20"

but i get string like this
"(10 )+(AL03-RM)+20.0(AL01-RM)+(30)+20.0"

i have tried different things, but can't get out put like
"(10)+(30)+20"
Please see image for details

Imgur: Image Link for Details[^]

Please help me to solve this.

What I have tried:

Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles SimpleButton2.Click

For i As Integer = 0 To dgv.Rows.Count - 1

'Dim paren As New Regex("\([^)]*\)")
'Dim paren As New Regex("\((.*?)\)")
Dim paren As New Regex("(?<=\().*?(?=\))")
Dim command As String = "((AL01-RM)+(AL03-RM))+20.0"

Dim matches As MatchCollection
matches = paren.Matches(command)

Dim m As Match
For Each m In matches

If m.ToString = dgv.Rows(i).Cells(1).Value Then
RTB2.AppendText(Regex.Replace(command, m.ToString, dgv.Rows(i).Cells(2).Value.ToString))

End If
Next

Next

End Sub

推荐答案

首先尝试让你对语言有所了解:它是C#或VB,而不是两者,从代码中,它是VB。



有一个工具可以让你看到代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]



在Visual Studio中调试C#代码 - YouTube [<一个href =https://www.youtube.com/watch?v=u-HdLtqEOogtarget =_ blanktitle =新窗口> ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它找不到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个bug。
First of all try to make your mind about the language: it is C# or VB, not both, from the code, it is VB.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这样的事情:
Dim paren As String = "\([^)]*\)"
Dim command As String = "(AL01-RM)+(AL03-RM)+20.0"
Dim matches As MatchCollection = Regex.Matches(command, paren)
		
For Each m In matches
Dim temp As String = "match = " + m.ToString()
Console.WriteLine(temp)
command = command.Replace(m.ToString(), "(xx)")
Next
	
Console.WriteLine(command)


这篇关于Vb.net:正则表达式查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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