Vb.net 如何从一行中选择一个单词?
基本上,我真的被卡住了,我想从指定行的句子中选择一个简单的单词(这是一个数字)。代码如下:Vb.net 如何从一行中选择一个单词?,vb.net,richtextbox,Vb.net,Richtextbox,基本上,我真的被卡住了,我想从指定行的句子中选择一个简单的单词(这是一个数字)。代码如下: Dim index As Integer = Me.RichTextBox1.Find("<h3>Current Guide Price <span title='") If index <> -1 Then Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIn
Dim index As Integer = Me.RichTextBox1.Find("<h3>Current Guide Price <span title='")
If index <> -1 Then
Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
If last = -1 Then last = Me.RichTextBox1.TextLength
Me.RichTextBox1.Select(first, last - first)
Me.RichTextBox1.SelectionBackColor = Color.DeepPink
Dim txxt As String
txxt = Me.RichTextBox1.SelectedText
Label1.Text = txxt
Dim index As Integer=Me.RichTextBox1.Find(“当前指导价一种可能是在所选文本上使用此正则表达式:
'(\d+(?:\.\d+)?)'
说明:
' : Looks for single quote character
\d+ : Digit [0-9] one or more time
\. : dot character
() : make a group
?: : mark group as non-capturing
? : makes the group optional
示例代码:(未经测试的代码提供了一个想法)
字符串可能存在不止一次吗?txxt,是的,它基本上是一个不完整的代码,我找不到结果。哇!从来没有听说过这些正则表达式ruby代码。我发现它们与你给我的代码在逻辑上是一样的,但是“匹配”和“正则表达式”“部件看起来没有定义。对于诊断,它似乎不是系统的一部分,我仍然会仔细检查。@J-sGrenier Add在代码文件的顶部导入system.Text.RegularExpressions
,以避免Match和RegeOptions出现问题。第二条语句应该是‘system.Diagnostics.Debug.Print(m.Tostring())”(“s”在诊断结束时)。同意@Blackwood。您可以在代码顶部指定名称空间,也可以按照我使用的方式使用名称空间。请尝试我编辑的答案。@Neverhopels,“m”是什么意思在这种情况下替换?@J-sGrenier,我希望Me.RichTextBox1.SelectedText
包含文本当前指导价513.7k
。在我的示例代码m.ToString()
中,将从所选文本中提取513.703
,这是您所需的结果。
Dim m as System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(Me.RichTextBox1.SelectedText, "'(\d+(?:\.\d+)?)'", System.Text.RegularExpressions.RegexOptions.None)
System.Diagnostics.Debug.Print(m.Tostring())