Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Complex string parsing
Message
De
19/03/2013 14:52:27
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01568786
Message ID:
01568800
Vues:
89
This message has been marked as the solution to the initial question of the thread.
J'aime (1)
>Hi everybody,
>
>This is my colleague's code:
>
>
>Regex regexXml = new Regex(@"\<(?'field'[^/].*?)\>(?'data'.*?)\</\k'field'\>");
>
>         MatchCollection matches = regexXml.Matches(tcSQML);
>
>What we want is to parse a string like this
>
>
>   String input = "<func>GetResLog</func><tcOperator>ADMIN</tcOperator><tcSalespoint>TICKETS</tcSalespoint><tnReservNo>1001001</tnReservNo><tcReturnType>DSP</tcReturnType>";
>
>into a dictionary of String, String where key represents the parameter's name and the value represents the value.
>
>The string above is simple and works nice with that RegEx expression.
>
>The problem is that we may have some nested XML like strings inside the parameters or even some chunks of XML (not necessary valid).
>
>So, the above doesn't work for the string like this
>
>
>String input = @"<func>getqtyremaining</func><tcoperator>ADMIN</tcoperator><tcreturntype>XML</tcreturntype>
><tcsalespoint>TICKET004001</tcsalespoint><ttdatetime>03/11/2013 01:19 PM</ttdatetime>
><TCDCILIST><DCI>ACTIVITIESSKYDIVING 1000      </DCI><DCI>ACTIVITIESSKYDIVING 1030      </DCI>
><DCI>ACTIVITIESSKYDIVING 1100      </DCI><DCI>ACTIVITIESSKYDIVING 1130      </DCI><DCI>ACTIVITIESSKYDIVING 1200      </DCI>
><DCI>ACTIVITIESSKYDIVING 1230      </DCI><DCI>ACTIVITIESSKYDIVING 1300      </DCI><DCI>ACTIVITIESSKYDIVING 1330      </DCI>
><DCI>ACTIVITIESSKYDIVING 1400      </DCI><DCI>ACTIVITIESSKYDIVING 1430      </DCI></TCDCILIST>";
>
>The whole current method is this
>
>
>public static void PopulateFromSQML(this Dictionary<String, String> tDictionary, String tcSQML)
>      {
>         Regex regexXml = new Regex(@"\<(?'field'[^/].*?)\>(?'data'.*?)\</\k'field'\>");
>
>         MatchCollection matches = regexXml.Matches(tcSQML);
>         foreach (Match m in matches)
>         {
>            try
>            {
>               if (!tDictionary.ContainsKey(m.Groups["field"].ToString()))
>                  tDictionary.Add(m.Groups["field"].ToString(), m.Groups["data"].ToString());
>            }
>            catch (Exception)
>            {
>               // Don't care
>            }
>         }
>      }
>
>Do you see what can I change in that method to be able to properly handle such strings?


(1) I've changed the pattern a bit - to my taste
(2) Use of the static Method
(3) Important - specify RegexOptions.Singleline
		public static void PopulateFromSQML(this Dictionary<String, String> tDictionary, String tcSQML)
		{
			tDictionary.Clear();

			string pattern = @"<(?<field>[^/>]+)>(?<data>.*)</\k<field>>";

			MatchCollection matches = Regex.Matches(tcSQML, pattern, RegexOptions.Singleline);
			foreach (Match m in matches)
			{
				//Console.WriteLine("Key: {0}\nValue: {1}\n", m.Groups["field"], m.Groups["data"]);

				if (!tDictionary.ContainsKey(m.Groups["field"].ToString()))
					tDictionary.Add(m.Groups["field"].ToString(), m.Groups["data"].ToString());
			}

		}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform