我是一个正则表达式的菜鸟。我有一堆要解析的用户代理字符串。
Windows Phone 搜索 (Windows Phone OS 7.10;Acer;Allegro;7.10;8860)
Windows Phone 搜索 (Windows Phone OS 7.10;HTC;7 Mozart T8698;7.10;7713)
Windows Phone 搜索 (Windows Phone OS 7.10;HTC;Radar C110e; 7.10;7720)
我如何使用正则表达式来提取:
A) Windows Phone 操作系统 7.10 宏碁 Allegro
B) Windows Phone 操作系统 7.10 HTC 7 莫扎特
C) Windows Phone OS 7.10 HTC 雷达
我尝试通过Split
以下方式使用,但无济于事:
private static string parse(string input)
{
input = input.Remove(0, input.IndexOf('(') + 1).Replace(')', ' ').Trim();
string[] temp = input.Split(';');
if (temp[2].Contains('T'))
{
temp[2] = temp[2].Substring(0, temp[2].IndexOf('T')).Trim();
}
StringBuilder sb = new StringBuilder();
sb.Append(temp[0] + " ");
sb.Append(temp[1] + " ");
sb.Append(temp[2]);
return sb.ToString();
}