我试图在一个文本块之间提取一个文本块而没有这样做。
文本块看起来像这样
!###################################################################################################
!# Approved : YES
!# REASON: sample reason
!# DEVICE: device1
!# ACL: Acl_sample
access-list <Rule No> extended permMuramat esp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
access-list <Rule No> extended permMuramat udp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
!##########################################################################
!# REASON:test
!# DEVICE: RTP
!# SECTION : Foundational
access-list <Rule No> permMuramat tcp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
!######################################################################################################
!# APPROVED : YES
!# REASON: test1xxx
!# DEVICE: xxx test1 Devices
!# SECTION: test1
!# REQUESTER: test1@test1test2.com
!# Exp. Date : 8/2016
access-list <Rule No> permMuramat tcp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
access-list <Rule No> permMuramat <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
基本上将所有内容都放入数组列表中。
我试图遵循一种模式,即起始索引将以&#34;!#&#34;开头。而下一个角色不是&#34;#&#34;然后这就成了&#34; Startindex&#34;如果下一个字符以&#34;!&#34;开头,那么结束索引将是并且进行中的角色至少有两个&#34;#&#34; es。
尽管失败但是:(
该文件基本上是一个rtf文件。这就是我的代码现在所看到的。
string path = @"\\WDMYCLOUD\Public\data.rtf";
string rtfText = File.ReadAllText(path);
RichTextBox rtBox = new RichTextBox { Rtf = rtfText };
var plainText = rtBox.Text;
var charArr = plainText.ToCharArray();
var arrBuffer = new ArrayList();
string tmp = "";
for (int i = 0; i < charArr.Length; i++)
{
int bufferIndex;
var startIndex = bufferIndex = i;
if (charArr[startIndex] == '!' && charArr[startIndex + 1] == '#' &&
charArr[startIndex + 2] != '#' && charArr[startIndex + 2] != ' ')
{
for (int j = bufferIndex; j < charArr.Length; j++)
{
if (charArr[j] == '!' && charArr[j + 1] == '#' && charArr[j + 2] == '#')
{
bufferIndex = j + 2;
for (int charStartIndex = startIndex; charStartIndex <= bufferIndex; charStartIndex++)
{
tmp = tmp + charArr[charStartIndex];
}
arrBuffer.Add(tmp);
tmp = "";
}
arrBuffer.Add(tmp);
tmp = "";
}
}
}
我在数组列表中得到了各种各样的混乱,而我希望它包含块。
预期输出应为
First Block:
!# Approved : YES
!# REASON: sample reason
!# DEVICE: device1
!# ACL: Acl_sample
access-list <Rule No> extended permMuramat esp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
access-list <Rule No> extended permMuramat udp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
Second Block:
!# REASON:test
!# DEVICE: RTP
!# SECTION : Foundational
access-list <Rule No> permMuramat tcp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
Third block:
!# APPROVED : YES
!# REASON: test1xxx
!# DEVICE: xxx test1 Devices
!# SECTION: test1
!# REQUESTER: test1@test1test2.com
!# Exp. Date : 8/2016
access-list <Rule No> permMuramat tcp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
access-list <Rule No> permMuramat <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>
有人可以帮我吗?
答案 0 :(得分:0)
也许你可以尝试另一种方法:
string rtfText = File.ReadAllText(path);
List<string> loStringList = new List<string>();
StringBuilder loBuilder = null;
foreach (var lsLine in Regex.Split(rtfText, Environment.NewLine))
{
if (lsLine.StartsWith("!##"))
{
if (loBuilder != null)
loStringList.Add(loBuilder.ToString());
loBuilder = new StringBuilder();
}
else if (loBuilder != null)
loBuilder.AppendLine(lsLine);
}
if (loBuilder != null)
loStringList.Add(loBuilder.ToString());
答案 1 :(得分:0)
我做了类似的事情,我做的是检查每一行是否包含某个字符,在你的情况下&#34; ##&#34;,如果它不包含它,将它添加到你的字符串/列表。
string[] lines = System.IO.File.ReadAllLines(@"[..]file.extension");
foreach (string line in lines)
if (!line.Contains("##")
{
//..
}
希望这有所帮助。