locked
string help needed RRS feed

  • Question

  • Hello

                    I am reading data on socket . I am getting following data 

     

    Message 1)   ProductID="125455" ProductName"Car";

    Message 2)   ProductName="Car" ProductID="125455";

     

    Note the Product ID position is not fixed. i want to read ProductID value which is 125455 

     

     


    It's Me
    Monday, May 23, 2011 1:18 PM

Answers

  • I'd try putting the strings into a dictionary.

    	class Program
    	{
    		static void Main( string[] args )
    		{
    			string text1 = "ProductID=\"125455\" ProductName\"Car\";";
    			string text2 = "ProductName=\"Car\" ProductID=\"125455\";";
    
    			Dictionary<string,string> result;
    			result = Parse( text1 );
    			Console.WriteLine(result["ProductID"]);
    			result = Parse( text2 );
    			Console.WriteLine(result["ProductID"]);
    		}
    
    		static Regex regex = new Regex("(\\w+)\\s*=?\\s*\"([^\"]*)\"");
    		static Dictionary<string,string> Parse( string text )
    		{
    			return regex.Matches( text ).Cast<Match>().ToDictionary( m => m.Groups[1].Value, m => m.Groups[2].Value );
    		}
    	}
    
    

    I generalized the expression a little bit to tolerate, the missing equal sign, the ; at the end, spaces between the quotes, spaces around the =.

    But I like the general technique of using a regular expression and then sticking the pairs into a dictionary.

     

    • Marked as answer by It_s Meee Tuesday, May 24, 2011 6:11 AM
    Monday, May 23, 2011 5:57 PM

All replies

  • Regular expressions are used for this kind of thing:

    using System;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        public static void Main(string[] args)
        {
          string text1 = "ProductID=\"125455\" ProductName\"Car\"";
          string text2 = "ProductName=\"Car\" ProductID=\"125455\";";
    
          Test(text1);
          Test(text2);
        }
    
        public static void Test(string text)
        {
          Console.WriteLine("Searching in: " + text);
    
          Regex regex = new Regex("ProductID=\"(\\d+)\"");
          Match match = regex.Match(text);
    
          if (match.Groups.Count == 2)
          {
            string productIdString = match.Groups[1].Value;
            int productId = int.Parse(productIdString);
    
            Console.WriteLine("Product ID = " + productId);
          }
          else
          {
            Console.WriteLine("Not found.");
          }
    
          Console.WriteLine();
        }
      }
    }
    
    
    See http://msdn.microsoft.com/en-us/library/hs600312.aspx for details.

    Monday, May 23, 2011 1:55 PM
  • Hi It's me

    Here is another solution if you discust the regular expressions

    using System;
    
    namespace MSDN
    {
      class Program
      {
        static void Main(string[] args)
        {
          // "ProductID=\"125455\" ProductName\"Car\"","ProductName=\"Car\" ProductID=\"125455\""
          int x = ExtarctorHelper("ProductID=\"125455\" ProductName\"Car\"");
          Console.WriteLine(x);
          Console.ReadLine();
        }
    
        static int ExtarctorHelper(string input)
        {
          int result=0;
          string[] splittedInput = input.Split('"');
          for (int i = 0; i < splittedInput.Length; i++)
          {
            bool flag = int.TryParse(splittedInput[i],out result);
            if (flag==true)
            {
              break;
            }
          }
          return result;
        }
      }
    }
    
    


    The complexity resides in the simplicity Follow me at: http://smartssolutions.blogspot.com
    Monday, May 23, 2011 3:44 PM
  • I'd try putting the strings into a dictionary.

    	class Program
    	{
    		static void Main( string[] args )
    		{
    			string text1 = "ProductID=\"125455\" ProductName\"Car\";";
    			string text2 = "ProductName=\"Car\" ProductID=\"125455\";";
    
    			Dictionary<string,string> result;
    			result = Parse( text1 );
    			Console.WriteLine(result["ProductID"]);
    			result = Parse( text2 );
    			Console.WriteLine(result["ProductID"]);
    		}
    
    		static Regex regex = new Regex("(\\w+)\\s*=?\\s*\"([^\"]*)\"");
    		static Dictionary<string,string> Parse( string text )
    		{
    			return regex.Matches( text ).Cast<Match>().ToDictionary( m => m.Groups[1].Value, m => m.Groups[2].Value );
    		}
    	}
    
    

    I generalized the expression a little bit to tolerate, the missing equal sign, the ; at the end, spaces between the quotes, spaces around the =.

    But I like the general technique of using a regular expression and then sticking the pairs into a dictionary.

     

    • Marked as answer by It_s Meee Tuesday, May 24, 2011 6:11 AM
    Monday, May 23, 2011 5:57 PM
  • hello

              it works thanks

     

    regards

     


    It's Me
    Tuesday, May 24, 2011 6:11 AM