【问题标题】:C# - find a line (regex) in a file and get the complete block of text according to another regexC# - 在文件中查找一行(正则表达式)并根据另一个正则表达式获取完整的文本块
【发布时间】:2012-04-30 19:08:38
【问题描述】:

问题是这样的:

我想在文本文件中找到一个正则表达式并获取完整的文本块

文字示例:

text text text text text text text text text 
!
title
text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text text 
!
text text text text text text text text text 

找到“标题”部分很容易,但我想得到以下结果:

title
text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text text 

最好的方法是什么?使用正则表达式模式或选择文本直到我得到“!”? (我想要简单/快速可读的代码)

查找模式的代码:(以 rtxtText 作为富文本框)

    private String searchInfo(String pattern)
    {
        String text = rtxtText.Text;
        Regex regExp = new Regex(pattern);
        String result = "";

        foreach (Match match in regExp.Matches(text))
        {
            result += "\n" + match.ToString();
        }
        return result; 
    }

【问题讨论】:

    标签: c# regex search find


    【解决方案1】:

    您的正则表达式也被更改为包含未知字符,例如

    • 第一title
    • 然后[^!]*[^ ] 表示不在此集合中的东西,所以[^!]* 是除! 之外的任何数字)

      正则表达式 regex = new Regex("title[^!]*", RegexOptions.SingleLine); MatcheCollection 匹配 = regex.Matches(text);

    【讨论】:

    • 以下使用正则表达式解决了我的问题:searchInfo(@"\ntitle[^!]*") 感谢您的帮助!
    • 不客气。如果您不希望结果中的起始换行符,您应该查看“lookbehind”:searchInfo(@"(?<=\n)title[^!]*");
    【解决方案2】:

    最好的方法是遍历文本行,直到找到第一个“!”然后收集,直到找到下一个:

    line = textfile.readline()
    while line and line.strip() != '!'
        line = textfile.readline() # skip until first '!'
    title = textfile.readline() # now on title line
    text = ''
    line = textfile.readline()
    while line and line.strip() != '!'
        text += line
        line = textfile.readline()
    print title
    print text
    

    【讨论】:

    • 对不起,但这绝对不是最好的方法。他只是想改变他的正则表达式来找到最多的文本!也是。
    • @Hinek,你可能是对的 - 我不太确定 OP 想要什么。对我来说,正则表达式似乎不是问题......
    • 我刚刚又看了一遍问题,不清楚。也许他只是想隔离,在行首的两个感叹号之间是什么。如果是这种情况,我会同意你的回答。让我们说现在轮到他澄清了......
    • 为了澄清问题:我想隔离以“title”开头并以“!”结尾的块。这通常用于配置文件。提供一些上下文:我的目的是根据此配置文件中存在的块分析配置文件。我正在搜索“title1”并想知道此块中存在的“parameter1”。为此,我需要根据开始参数“title”和结束参数“!”来隔离这个块。我要感谢您的回答!
    【解决方案3】:
    public IEnumerable<string> ParseParagraphs(string text)
    {
        Regex regex = new Regex(@"title[^!]*");
        foreach (Match match in regex.Matches(text))
            yield return match.Value;  
    }
    

    用法很简单:

    foreach (var p in ParseParagraphs(your_text))
        Console.WriteLine(p);
    

    更新:在 SearchInfo 方法中使用 StringBuilder 以避免在内存中创建许多字符串

    private string SearchInfo(String pattern)
    {            
        MatchCollection matches = Regex.Matches(rtxtText.Text, pattern);
        if (matches.Count == 0)
            return String.Empty;
    
        StringBuilder sb = new StringBuilder();
        foreach (Match match in matches)
            sb.AppendLine(match.Value);
    
        return sb.ToString();
    }
    

    这样称呼var result = SearchInfo(@"title[^!]*");

    【讨论】:

    • 感谢您的回答,这帮助我改进了代码!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2013-07-13
    相关资源
    最近更新 更多