我正在使用TCPClient通过LAN发送消息(数组列表),所以我分开了:
string arr_sep = "[s{p(a)c}e]";
string list_sep = "[n|e|w)";
如何使用正则表达式将以下string
行解译为List<string[]>
?
string tessst = "abra[s{p(a)c}e]kada[s{p(a)c}e]bra[n|e|w)hel[s{p(a)c}e]oww[s{p(a)c}e]een";
这是我尝试做的事情:
string tessst = "abra[s{p(a)c}e]kada[s{p(a)c}e]bra[n|e|w)hel[s{p(a)c}e]oww[s{p(a)c}e]een";
List<string[]> splited2 = new List<string[]>();
if (tessst.Length > 0)
{
List<string> splited1 = new List<string>(Regex.Split(tessst, "[^a-zA-Z]+")); //[s{p(a)c}e]
for (int i = 0; i < splited1.Count; i++)
{
splited2.Add(Regex.Split(splited1[i], "[^a-zA-Z]+")); // [n|e|w)
}
}
//splited2 is the result!
不幸的是,正则表达式完全崩溃了 - 我该如何修复它?是否有更好的方法?
预期结果:
List<string[]> result = new List<string[]>();
result.Add(new string[]{"abra", "kada", "bra"});
result.Add(new string[]{"hel", "oww", "een"});
编辑:修复
当我收到数据时 - 我通常会将字节数限制为1024,但这还不足以获得List<string[]>
的所有50个条目!
我将字节数增加到10000,现在所有信息都通过LAN!序列化50 string[]
List<string[]>
需要3499个字节。将来我将在我的列表中使用多达900个条目,因此可以安全地假设我需要:
(3499/50)* 900 = 63000字节来序列化我的数据!!
问题是 - 一次发送必须数据是否安全/可靠?这是我用来接收的代码:
string message = "";
int thisRead = 0;
int max = 10000; // from 1024 to 10000
Byte[] dataByte = new Byte[max];
using (var strm = new MemoryStream())
{
thisRead = Nw.Read(dataByte, 0, max);
strm.Write(dataByte, 0, thisRead);
strm.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(strm))
{
message = reader.ReadToEnd();
}
}
List<string[]> result = new JavaScriptSerializer().Deserialize<List<string[]>>(message );
那是发送:
List<string[]> list= new List<string[]>();
list = browser_ex.GetMusicListSer(); // 50 list elements
string text_message = new JavaScriptSerializer().Serialize(list);
MemoryStream Fs = new MemoryStream(ASCIIEncoding.Default.GetBytes(text_message));
Byte[] buffer = Fs.ToArray();
Nw.Write(buffer, 0, buffer.Length); // 3499 bytes
我可以将最大字节数增加到 100,000 并一劳永逸地忘记这个问题吗?应该有另一种解决方案......我相信。
答案 0 :(得分:2)
使用序列化
,而不是重新发明轮子你有很多这个的选择(JavaScriptSerializer,DataContractSerializer,DataContractJsonSerializer,BinaryFormatter,SoapFormatter,XmlSerializer)。
List<string[]> list = new List<string[]>();
list.Add(new string[] { "abra", "kada", "bra" });
list.Add(new string[] { "hel", "oww", "een" });
string stringToSend = new JavaScriptSerializer().Serialize(list);
//Send
string receivedString = stringToSend;
List<string[]> result = new JavaScriptSerializer()
.Deserialize<List<string[]>>(receivedString);
<强>»EDIT«强>
假设Nw
为NetworkStream
,您的代码可以像这样简单:
//Receiver
StreamReader reader = new StreamReader(Nw);
while (true)
{
List<string[]> result = new JavaScriptSerializer()
.Deserialize<List<string[]>>(reader.ReadLine());
//do some work with "result"
}
//Sender
StreamWriter writer = new StreamWriter(Nw);
while (true)
{
//form your "list" and send
writer.WriteLine(new JavaScriptSerializer().Serialize(list));
writer.Flush();
}
答案 1 :(得分:1)
string tessst = "abra[s{p(a)c}e]kada[s{p(a)c}e]bra[n|e|w)hel[s{p(a)c}e]oww[s{p(a)c}e]een";
List<string[]> splited2 = new List<string[]>();
if (tessst.Length > 0)
{
List<string> splited1 = new List<string>(Regex.Split(tessst, @"\[n\|e\|w\)"));
for (int i = 0; i < splited1.Count; i++)
{
splited2.Add(Regex.Split(splited1[i], @"\[s\{p\(a\)c\}e\]"));
}
}
这将为您提供所描述的所需输出。
答案 2 :(得分:0)
如果您确定不想使用任何现有协议或技术,可以使用BinaryWriter和BinaryReader类轻松创建自己的协议:
发信人:
using (var writer = new BinaryWriter(networkStream))
{
foreach (var array in list)
{
writer.Write(array.Length);
for (int i = -; i < array.Length; i++)
{
writer.Write(array[i]);
}
}
writer.Write(0);
writer.Flush();
}
接收器:
using (var reader = new BinaryReader(networkStream))
{
var list = new List<string[]>();
int length;
while ((length = reader.ReadInt32()) != 0)
{
var array = new string[length];
for (int i = 0; i < array.Length; i++)
{
array[i] = reader.ReadString();
}
list.Add(array);
}
}