I am a total noob to regex. I have a bunch of user agent strings that I want to parse.
Windows Phone Search (Windows Phone OS 7.10;Acer;Allegro;7.10;8860)
Windows Phone Search (Windows Phone OS 7.10;HTC;7 Mozart T8698;7.10;7713)
Windows Phone Search (Windows Phone OS 7.10;HTC;Radar C110e;7.10;7720)
How can I use regex to just extract:
A) Windows Phone OS 7.10 Acer Allegro
B) Windows Phone OS 7.10 HTC 7 Mozart
C) Windows Phone OS 7.10 HTC Radar
I have tried to use Split in the following way but to no avail:
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();
}
String.Split? Or you want to learn regular expressions (than your question should be worded differently)...Splithere for convenience, the real question is what do you want to happen to strings that do not exactly match these patterns? For example, note that "Allegro" is a token by its own while "Mozart" and "Radar" both have secondary tokens that you don't want to keep. What if you have a UA string with three tokens in that position? Or four? Or none?