我如何 select 一行中的一个词?
How can I select a word from a line?
基本上我真的卡住了,我想 select 从指定行的句子中提取一个简单的单词(这是一个数字)。这是代码:
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
如图所示,我想要 label1 中的 513.703 而不是整行 "Current Guide Price 513.7k" 如果您能帮助我,我将不胜感激!
一种可能性是在所选文本上使用此正则表达式:
'(\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
Demo
示例代码:(未经测试的代码给你一个想法)
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())
基本上我真的卡住了,我想 select 从指定行的句子中提取一个简单的单词(这是一个数字)。这是代码:
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
如图所示,我想要 label1 中的 513.703 而不是整行 "Current Guide Price 513.7k" 如果您能帮助我,我将不胜感激!
一种可能性是在所选文本上使用此正则表达式:
'(\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
Demo
示例代码:(未经测试的代码给你一个想法)
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())