3 回答
TA贡献1815条经验 获得超13个赞
我会为此建议简单的正则表达式。看看这个例子:
using System.Diagnostics;
using System.Text.RegularExpressions;
...
Regex regex = new Regex("<A>(.*?)<B><BEOF>(.*?)<AEOF>");
string myString = @"<START><A>message<B><BEOF>UnknownLengthOfText<AEOF><A>message<B><BEOF>some other line of text<AEOF><END>";
MatchCollection matches = regex.Matches(myString);
foreach (Match m in matches)
{
Debug.WriteLine(m.Groups[1].ToString(), m.Groups[2].ToString());
}
编辑:
由于字符串在一行中,正则表达式应该是“懒惰的”,用惰性量词标记?。此外,我更改了正则表达式,以便它也使用 sTrenat 的建议来自动解析消息和标题。
所以,而不是
Regex regex = new Regex("<A>(.*)<AEOF>");
我用了
Regex regex = new Regex("<A>(.*?)<B><BEOF>(.*?)<AEOF>");
注意附加的?标记惰性量词,当它找到标签之间的第一个匹配时停止(没有?整个 strign 将被捕获,而不是n标签之间的消息)
TA贡献1794条经验 获得超8个赞
用这样的东西试试:
string test = @"<START>
<A>message<B><BEOF>UnknownLengthOfText<AEOF>
<A>message<B><BEOF>UnknownLengthOfText<AEOF>
<END>";
//for this test this will give u an array containing 3 items...
string[] tmp1 = test.Split("<AEOF>");
//here u will store your results in
List<string> results = new List<string>();
//for every single one of those 3 items:
foreach(string item in tmp1)
{
//this will only be true for the first and second item
if(item.Contains("<A>"))
{
string[] tmp2 = item.Split("<A>");
//As the string you are looking for is always BEHIND the <A> you
//store the item[1], (the item[0] would be in front)
results.Add(tmp2[1]);
}
}
TA贡献1725条经验 获得超7个赞
而不是使用 String.Split 你可以使用 Regex.Split 如下
var stringToSplit = @"<START>
<A>message<B>UnknownLengthOfText<BEOF><AEOF>
<A>message<B>UnknownLengthOfText<BEOF><AEOF>
<END>";
var regex = "<A>(.*)<AEOF>";
var splitStrings = Regex.Split(stringToSplit, regex);
splitStrings 将包含 4 个元素
splitString[0] = "<START>"
splitString[1] = "message<B>UnknownLengthOfText<BEOF>"
splitString[2] = "message<B>UnknownLengthOfText<BEOF>"
splitString[3] = "<END>"
玩正则表达式只能给你和之间的字符串
- 3 回答
- 0 关注
- 178 浏览
添加回答
举报