【问题标题】:Regex Expression: Capture the Text Between the Lines which includes Newline, Spaces, And Underscore characters正则表达式:捕获行之间的文本,包括换行符、空格和下划线字符
【发布时间】:2019-06-26 14:35:28
【问题描述】:

我从一个 pdf 文件中提取了一些文本并读入了一个字符串:

...

Fabric Business Of the Cloths 

4 Description of the property being purchased 
______________________________________________________________________________

...

我想提取4 Description of the property being purchased 行之前的单词,而不是它上面的任何东西或它下面的下划线行。

我尝试使用正则表达式 /^[^4]*/ 但这返回 null。

实现上述目标的合适正则表达式是什么?

谢谢。

【问题讨论】:

  • 要使用正则表达式,您需要识别一些正则模式。由于信息如此之少,很难知道您的数据是如何组成的以及可以使用哪些常规模式。请提供更完整的输入示例。
  • 我会使用 StreamReader 读取文件。因此,您希望在“织物业务”行之后立即行。通常你想在你想要的行之前而不是之后键入行。
  • 标题中的“包含换行符”是什么意思? “上面没有任何东西”是什么意思?

标签: c# .net vb.net uipath


【解决方案1】:

您的正则表达式有效,只需删除开头和结尾的 /。

示例

    private void TestRegex()
    {
        string s = "...\n Fabric Business Of the Cloths\n                         4 Description of the property being purchased\n____________________________________________________________________________\n ...";
        Regex regex = new Regex("^[^4]*"); // <--- DO LIKE THIS, PERHAPS.
        //Regex regex = new Regex("/^[^4]*/"); <----NOT THIS
        Match match = regex.Match(s, 0);
        if (match.Success)
        {
            Console.WriteLine(match.Value);
        }
    }

输出

...
 Fabric Business Of the Cloths

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多