【问题标题】:Removing text between "--" and "\cr"删除“--”和“\cr”之间的文本
【发布时间】:2012-05-16 10:03:54
【问题描述】:

我想删除“--”和“\cr”之间的文本。

我实际上是在读取一个文件,如果文件中有一个“--”,它应该删除“--”以及“\cr”之前的所有内容。

我正在逐行读取文件。

using (StreamReader readFile = new StreamReader(filePath))
{
    string line;

    while ((line = readFile.ReadLine()) != null)
    {
    }
}

我尝试使用子字符串来查找字符

line.Substring(line.IndexOf("--"),line.IndexOf("\cr"));

但是我在寻找每一行的分隔符时遇到了问题

我正在考虑写这样的东西

while ((line = readFile.ReadLine()) != null)
{
    if (line.Substring(line.IndexOf("--")) // If it has "--"
    {
      //Then remove all text from between the 2 delimiters

    }
}

请帮忙

谢谢

编辑:

问题已解决,虽然我遇到了另一个问题,但我无法删除 /* */ 之间的 cmets,因为 cmets 出现在多行上。所以我需要删除/* */之间的所有文本。

有什么建议或帮助吗? 谢谢

【问题讨论】:

  • 那个“\cr”看起来有些奇怪。如果你的意思是回车中的 CR,它写成“\r”,但已经被 ReadLine 剥离,这样只需删除“--”上的所有内容。
  • 谢谢尤金,你说得对。应该是回车
  • 必须发布什么样的问题才能获得UpVoted?

标签: c# .net wpf newline streamreader


【解决方案1】:

一个简单的解决方案是直接使用正则表达式替换:

line = Regex.Replace(line, @"--.*$", "");

这假设您对 \cr 的意思是实际的行尾(如果您使用 ReadLine() 阅读它,则无论如何都不包括在内),因此这会删除从 -- 到行尾的所有内容换行。

您也可以使用替换/* ... */ cmets:

line = Regex.Replace(line, @"--.*$|/\*.*?\*/", "");

快速 PowerShell 测试:

PS> $a = 'foo bar','foo bar -- some comment','foo /* another comment */ bar'
PS> $a -replace '--.*$|/\*.*?\*/'
foo bar
foo bar
foo  bar

【讨论】:

  • 我会试试 regex.replace() 方法。但是 .*$ 是什么意思?每当我想在正则表达式中使用字符时,我是否必须将其放入?
  • 你的回答很好,除了现在我遇到了 /* */ cmets 的问题
  • 我设法删除了 '/*' 但没有删除 '*/'
  • 它们是否延伸到多行?
【解决方案2】:

试试这个

line.Substring(line.IndexOf("--"));

正如 Joey 所说,ReadLine() 永远不会包含 Environment.NewLine 和 \cr 对应于 Environment.NewLine

【讨论】:

    【解决方案3】:

    只是为了展示如何从文件的每一行中删除 cmets。这是一种方式:

    var newLines = from l in File.ReadAllLines(path)
                   let indexComment =  l.IndexOf("--")
                   select indexComment == -1 ? l : l.Substring(0, indexComment);
    File.WriteAllLines(path, newLines);      // rewrite all changes to the file
    

    编辑:如果您还想删除/**/ 之间的所有内容,这是一个可能的实现:

    String[] oldLines = File.ReadAllLines(path);
    List<String> newLines = new List<String>(oldLines.Length);
    foreach (String unmodifiedLine in oldLines)
    {
        String line = unmodifiedLine;
        int indexCommentStart = line.IndexOf("/*");
        int indexComment = line.IndexOf("--");
    
        while (indexCommentStart != -1 && (indexComment == -1 || indexComment > indexCommentStart))
        {
            int indexCommentEnd = line.IndexOf("*/", indexCommentStart);
            if (indexCommentEnd == -1)
                indexCommentEnd = line.Length - 1;
            else
                indexCommentEnd += "*/".Length;
            line = line.Remove(indexCommentStart, indexCommentEnd - indexCommentStart);
            indexCommentStart = line.IndexOf("/*");
        }
    
        indexComment = line.IndexOf("--");
        if (indexComment == -1)
            newLines.Add(line);
        else
            newLines.Add(line.Substring(0, indexComment));
    }
    
    File.WriteAllLines(path, newLines);
    

    【讨论】:

    • 嗨蒂姆,如果你想删除 /* "" */.中间的文本出现在多行上。我想删除它是 commeting
    • @Sigh-AniDe:你还没有在你的问题中提到这个。
    • 我已将问题添加到我的问题中:?
    • @Sigh-AniDe:编辑了我的答案,将/* "" */ 考虑在内。
    【解决方案4】:

    您似乎想忽略包含 cmets 的行。怎么样

    if (!line.StartsWith("--")) { /* do stuff if it's not a comment */ }
    

    甚至

    if (!line.TrimStart(' ', '\t').StartsWith("--")) { /* do stuff if it's not a comment */ }
    

    忽略行首的空格。

    【讨论】:

    • 我没有得到这样的印象,即这些行必须以--开始。否则他们不会使用IndexOf
    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多