如何从一行中选择一个单词?

时间:2015-09-14 19:50:27

标签: 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.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

enter image description here

就像我在图片中所示,我希望标签1中的513.703而不是整行“Current Guide Price 513.7k”如果你能帮助我,我会非常感激它!

1 个答案:

答案 0 :(得分:1)

可能是在所选文本上使用此正则表达式:

'(\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())