开发者

Match beginning of file to string literal

开发者 https://www.devze.com 2023-03-04 18:34 出处:网络
I\'m working with a multi line text block where I need to divide everything into 3 groups 1: beginning of the file up to a string literal // don\'t keep

I'm working with a multi line text block where I need to divide everything into 3 groups

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
 <<

I'm finding plenty of ways to pull from the beginning of a line but am unable to see how to include an unknown number of non-blank lines until I reach that string literal.

EDIT: I'm removing the .net-ness to focus on regex only. Perhaps this is a place for understanding b开发者_如何学Pythonackreferences?


Rather than read the entire file into memory, just read what you need:

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;
}

I suppose there's a LINQ solution, although I don't know what it is.

EDIT:

If you already have the text of the email in you application (as a string), you have to split it into lines first. You can do that with String.Split, splitting on newlines, or you can create a StringReader and read it line-by-line. The logic above still applies, but rather than File.ReadLines, just use foreach on the array of lines.

EDIT 2:

The following LINQ might do it:

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

Or, if the strings are already in a list:

TopLines = lines.TakeWhile(s => s != Literal).ToList();
TopLines.Add(Literal);


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


File.ReadAllLines() will give you an array you can iterate over until you find your literal, then take the next line

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

精彩评论

暂无评论...
验证码 换一张
取 消