我需要解析Windows文本文件并提取与操作相关的所有数据。操作 用$ OPERATION和$ OPERATION_END分隔。我需要做的是为所有操作提取所有文本块。我怎样才能有效地做到这一点 使用正则表达式或简单的String方法。我很感激你提供的小片段。
$OPERS_LIST
//some general data
$OPERATION
//some text block
$OPERATION_END
$OPERS_LIST_END
答案 0 :(得分:1)
try {
if (Regex.IsMatch(subjectString, @"\$OPERATION(.*?)\$OPERATION_END", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace)) {
// Successful match
} else {
// Match attempt failed
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
答案 1 :(得分:1)
尝试这样的扩展方法。只需传入与您正在阅读的文件对应的TextReader
。
public static IEnumerable<string> ReadOperationsFrom(this TextReader reader)
{
if (reader == null)
throw new ArgumentNullException("reader");
string line;
bool inOperation = false;
var buffer = new StringBuilder();
while ((line = reader.ReadLine()) != null) {
if (inOperation) {
if (line == "$OPERATION")
throw new InvalidDataException("Illegally nested operation block.");
if (line == "$OPERATION_END") {
yield return buffer.ToString();
buffer.Length = 0;
inOperation = false;
} else {
buffer.AppendLine(line);
}
} else if (line == "$OPERATION") {
inOperation = true;
}
}
if (inOperation)
throw new InvalidDataException("Unterminated operation block.");
}
答案 2 :(得分:1)
从列表中获取所有操作:
var input = @"$OPERS_LIST
//some general data
$OPERATION
erfgergwerg
ewrg//some text block
$OPERATION_END
$OPERATION
//some text block
$OPERATION_END
$OPERATION
//some text block
$OPERATION_END
$OPERS_LIST_END";
foreach (Match match in Regex.Matches(input, @"(?s)\$OPERATION(?<op>.+?)\$OPERATION_END"))
{
var operation = match.Groups["op"].Value;
// do something with operation...
}