【问题标题】:Regex pattern to extract row用于提取行的正则表达式模式
【发布时间】:2015-05-30 13:41:12
【问题描述】:

我有如下文本文件:

some text 402115000518432  
402115000518432  97517518878      IDLE
some text
...
some text 402115001509990  
402115001509990  97517490827      IDLE
...

我想要在 excel 文件中以 40211“开始”的整行。期望的输出:

402115000518432  97517518878      IDLE
402115001509990  97517490827      IDLE

我正在尝试使用以下代码:

Dim pattern = "(?<=\s*)40211[^\s]*"
Dim i = 1

For Each line In File.ReadLines(RichTextBox1.Text)
            Dim match = Regex.Match(line, pattern)


            If match.Success Then
                sheet.Cells(i, 1).Value = match.Value
                i += 1
            End If
        Next

但输出只是以 40211 开头的值,而不是以 40211 开头的整行。感谢任何帮助。

【问题讨论】:

    标签: regex vb.net


    【解决方案1】:
            pattern = "^(40211)"
    
    
            If match.Success Then
                sheet.Cells(i, 1).Value = line
                i += 1
            End If
    

    你几乎破解了它。只需重用 line 变量即可。

    【讨论】:

    • 谢谢。有用。但是我现在有个问题。我已经编辑了我的问题。 40211 个字符也出现在其他地方。但我正在寻找以 40211 开头的行。我已经编辑了这个问题。有什么想法吗?
    • @slyclam 试试pattern = "^(40211)"
    • 您介意为我回答这个问题吗? stackoverflow.com/questions/30543061/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多