English 中文(简体)
档案的开端以说明字面
原标题:Match beginning of file to string literal
  • 时间:2011-05-07 13:21:00
  •  标签:
  • regex

I m与多行文组合作,我需要把一切分成三组

1: beginning of the file up to a string literal // don t keep
2: The next line //KEEP THE LINE FOLLOWING STRING LITERAL
3: Everything following that line to the end of file. // don t keep


 <<
 aFirstLing here 
 aSecondLine here
 MyStringLiteral  //marks the next line as the target to keep 
 What I want to Keep!
 all kinds of crap that I don t
 <<

我发现,从一线一开始,有无数的路要走,但我无法看到,在我达到这一字面之前,如何列入数目不明的非空白线。

EDIT:I m 删除蚊帐,只注重ex。 或许这是了解反馈的场所?

最佳回答

仅读到你们需要的东西,而不是读到整个档案中:

List<string> TopLines = new List<string>();
string prevLine = string.Empty;
foreach (var link in File.ReadLines(filename))
{
    TopLines.Add(line);
    if (prevLine == Literal)
    {
        break;
    }
    prevLine = line;
}

尽管我不知道是谁,但我还是会支持解决该问题。

EDIT:

如果你在申请中已有电子邮件文本(作为示意图),你必须首先将其分为几行。 您可以通过<代码>String. Split来做到这一点,在新线路上分立,或可创建<代码>StringReader并逐行读。 以上逻辑仍然适用,但并不适用<代码>File.ReadLines,仅使用foreach

EDIT:

The following LINQ may do it:

TopLines = File.ReadLines(filename).TakeWhile(s => s != Literal).ToList();
TopLines.Add(Literal);

或者,如果指示已经列入清单:

TopLines = lines.TakeWhile(s => s != Literal).ToList();
TopLines.Add(Literal);
问题回答

.*(^MyStringLiteral ? )([w|s][^ ]+)(.+) seems to work. the trick wasn t back references - it was the exclusion of .

文件:Read AllLines(Read AllLines)将给你一个阵列,在你找到字面之前,你可以接手。

string[] lines = File.ReadAllLines();
for(int i;i<lines.Length;i++)
{
     if(line == Literal)
     return lines[i + 1];
}




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签