none
Help in Regular expressions

    Question

  • hi

    can any one help me in this

     i want regular expression to take each element a lone

    the text is like this

    +CMGL: 0,"REC UNREAD",6,27,"+90000000000",145,"12/01/02,05:53:52-20","12/01/02,
    23:10:49-20",0

    i need each element alone without , or "" 

    • Changed type Martin_XieModerator Wednesday, January 04, 2012 2:49 AM Change to question so that more beginners can search out it.
    Tuesday, January 03, 2012 10:28 AM

Answers

  • StringBuilder testFile = new StringBuilder();
    testFile.AppendLine(@"+CMGL: 0,""REC UNREAD"",6,27,""+971506178629"",145,""12/01/02,05:53:52-20"",""12/01/02,""23:10:49-20"",0");
    testFile.AppendLine(@"+CMGL: 1,""REC UNREAD"",6,47,""+971506178649"",145,""12/01/02,05:55:11-20"",""12/01/03,03:51:30-20"",0");
    testFile.AppendLine(@"+CMGL: 2,""REC UNREAD"",6,76,""+971506178677"",145,""12/01/02,06:01:05-20"",""12/01/02,23:46:46-20"",0");
    testFile.AppendLine(@"+CMGL: 3,""REC UNREAD"",2,98,""+971506186817"",145,""12/01/01,16:51:57-20"",""12/01/02, 16:51:58-20"",70");
    testFile.AppendLine(@"+CMGL: 4,""REC UNREAD"",2,99,""+971506186818"",145,""12/01/01,16:52:01-20"",""12/01/02,16:52:01-20"",70");
    testFile.AppendLine(@"+CMGL: 5,""REC UNREAD"",2,123,""+971506186842"",145,""12/01/01,16:54:09-20"",""12/01/02,16:54:10-20"",70");
    testFile.AppendLine(@"+CMGL: 6,""REC UNREAD"",2,143,""+971506186861"",145,""12/01/01,16:57:27-20"",""12/01/02,16:57:28-20"",70");
    testFile.AppendLine(@"+CMGL: 7,""REC UNREAD"",6,150,""+971506186868"",145,""12/01/01,16:57:55-20"",""12/01/02,16:57:56-20"",70");
    
    String[] split = testFile.ToString().Split(new Char[]{','}, StringSplitOptions.RemoveEmptyEntries);
    


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    Tuesday, January 03, 2012 4:41 PM
  • Or alternatively:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String testWord = String.Format(@"+CMGL: 0,""REC UNREAD"",6,27,""+971506178629"",145,""12/01/02,05:53:52-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"23:10:49-20"",0""{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 1,""REC UNREAD"",6,47,""+971506178649"",145,""12/01/02,05:55:11-20"",""12/01/03,{0}", Environment.NewLine);
                testWord += String.Format(@"03:51:30-20"",0{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 2,""REC UNREAD"",6,76,""+971506178677"",145,""12/01/02,06:01:05-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"23:46:46-20"",0{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 3,""REC UNREAD"",2,98,""+971506186817"",145,""12/01/01,16:51:57-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"16:51:58-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 4,""REC UNREAD"",2,99,""+971506186818"",145,""12/01/01,16:52:01-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"16:52:01-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 5,""REC UNREAD"",2,123,""+971506186842"",145,""12/01/01,16:54:09-20"",""12/01/02{0}", Environment.NewLine);
                testWord += String.Format(@",16:54:10-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 6,""REC UNREAD"",2,143,""+971506186861"",145,""12/01/01,16:57:27-20"",""12/01/02{0}", Environment.NewLine);
                testWord += String.Format(@",16:57:28-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 7,""REC UNREAD"",6,150,""+971506186868"",145,""12/01/01,16:57:55-20"",""12/01/02{0}", Environment.NewLine);
                testWord += @",16:57:56-20"",70";
    
                String pattern = @"(?<a>\+CMGL):[ ](\d*),\""
                                 (?<b>[^\""]*)"",
                                 (?<c>\d*),(?<d>\d*),\""
                                 (?<e>\+\d*)"",(?<f>\d*),\""
                                 (?<g>\d*\/\d*\/\d*,\d{2}:\d{2}:\d{2}\-\d{2})\"",\""
                                 (?<h>\d{2}\/\d{2}\/\d{2}),?\r\n,?
                                 (?<i>\d{2}:\d{2}:\d{2}\-\d{2})\"",
                                 (?<j>\d*)";
    
                Regex rx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
                Match m = rx.Match(testWord);
                while (m.Success)
                {
                    Console.WriteLine(m.Groups["a"].Value);
                    Console.WriteLine(m.Groups["b"].Value);
                    Console.WriteLine(m.Groups["c"].Value);
                    Console.WriteLine(m.Groups["d"].Value);
                    Console.WriteLine(m.Groups["e"].Value);
                    Console.WriteLine(m.Groups["f"].Value);
                    Console.WriteLine(m.Groups["g"].Value);
                    Console.WriteLine(m.Groups["h"].Value);
                    Console.WriteLine(m.Groups["i"].Value);
                    Console.WriteLine(m.Groups["j"].Value);
                    Console.WriteLine("");
                    m = m.NextMatch();
                }
                Console.ReadLine();
            }
        }
    }
    
    



    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    Tuesday, January 03, 2012 6:17 PM

All replies



  • add this code in the button code area:

    string[] s;
                s = textBox1.Text.ToString().Split(',');
                foreach (string str in s)
                    listBox1.Items.Add(str.Trim('\"'));


    this will split the text to array of stand alone parts of your above text and show them in the listbox.

     


    Tuesday, January 03, 2012 10:44 AM
  • yes thank you

    but this is for one line

    how can i use this for multie lines with the same format

    my text is multilines of the above format

    how can i use your code to get what i need?

    Tuesday, January 03, 2012 11:15 AM
  • does this works??

    Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");

    Tuesday, January 03, 2012 11:24 AM
  • Give me more details, or u can paste the whole code to give me more space of understanding
    Tuesday, January 03, 2012 11:39 AM
  • +CMGL: 0,"REC UNREAD",6,27,"+971506178629",145,"12/01/02,05:53:52-20","12/01/02,
    23:10:49-20",0
    +CMGL: 1,"REC UNREAD",6,47,"+971506178649",145,"12/01/02,05:55:11-20","12/01/03,
    03:51:30-20",0
    +CMGL: 2,"REC UNREAD",6,76,"+971506178677",145,"12/01/02,06:01:05-20","12/01/02,
    23:46:46-20",0
    +CMGL: 3,"REC UNREAD",2,98,"+971506186817",145,"12/01/01,16:51:57-20","12/01/02,
    16:51:58-20",70
    +CMGL: 4,"REC UNREAD",2,99,"+971506186818",145,"12/01/01,16:52:01-20","12/01/02,
    16:52:01-20",70
    +CMGL: 5,"REC UNREAD",2,123,"+971506186842",145,"12/01/01,16:54:09-20","12/01/02
    ,16:54:10-20",70
    +CMGL: 6,"REC UNREAD",2,143,"+971506186861",145,"12/01/01,16:57:27-20","12/01/02
    ,16:57:28-20",70
    +CMGL: 7,"REC UNREAD",6,150,"+971506186868",145,"12/01/01,16:57:55-20","12/01/02
    ,16:57:56-20",70

     

    this is the resulted text of some query

    each line is a recieved message through gsm modem and contains many information i need

     

    i used the code below

     Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
                    Match m = r.Match(input);
                    while (m.Success)
                    {
                        ShortMessage msg = new ShortMessage();
                        msg.Index = int.Parse(m.Groups[1].Value);
                        msg.Index = m.Groups[1].Value;
                        msg.Status = m.Groups[2].Value;
                        msg.Sender = m.Groups[3].Value;
                        msg.Alphabet = m.Groups[4].Value;
                        msg.Sent = m.Groups[5].Value;
                        msg.Message = m.Groups[6].Value;
                        messages.Add(msg);
                       
                        m = m.NextMatch();
                    }

     

    but it doesnt work , i think the regular expression is not right

    could you fix it plz

    Tuesday, January 03, 2012 11:51 AM
  • Check Reg Exp Builder.  http://cybernetnews.com/online-regular-expression-builder/

    Werewolf,


    Just a newbie for everything.
    Tuesday, January 03, 2012 11:51 AM
  • i couldn't get the right expression 

    please help me in this 

    i want the right expression that gets all elements in the string 

    this is urgent plz

    Tuesday, January 03, 2012 3:51 PM
  • StringBuilder testFile = new StringBuilder();
    testFile.AppendLine(@"+CMGL: 0,""REC UNREAD"",6,27,""+971506178629"",145,""12/01/02,05:53:52-20"",""12/01/02,""23:10:49-20"",0");
    testFile.AppendLine(@"+CMGL: 1,""REC UNREAD"",6,47,""+971506178649"",145,""12/01/02,05:55:11-20"",""12/01/03,03:51:30-20"",0");
    testFile.AppendLine(@"+CMGL: 2,""REC UNREAD"",6,76,""+971506178677"",145,""12/01/02,06:01:05-20"",""12/01/02,23:46:46-20"",0");
    testFile.AppendLine(@"+CMGL: 3,""REC UNREAD"",2,98,""+971506186817"",145,""12/01/01,16:51:57-20"",""12/01/02, 16:51:58-20"",70");
    testFile.AppendLine(@"+CMGL: 4,""REC UNREAD"",2,99,""+971506186818"",145,""12/01/01,16:52:01-20"",""12/01/02,16:52:01-20"",70");
    testFile.AppendLine(@"+CMGL: 5,""REC UNREAD"",2,123,""+971506186842"",145,""12/01/01,16:54:09-20"",""12/01/02,16:54:10-20"",70");
    testFile.AppendLine(@"+CMGL: 6,""REC UNREAD"",2,143,""+971506186861"",145,""12/01/01,16:57:27-20"",""12/01/02,16:57:28-20"",70");
    testFile.AppendLine(@"+CMGL: 7,""REC UNREAD"",6,150,""+971506186868"",145,""12/01/01,16:57:55-20"",""12/01/02,16:57:56-20"",70");
    
    String[] split = testFile.ToString().Split(new Char[]{','}, StringSplitOptions.RemoveEmptyEntries);
    


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    Tuesday, January 03, 2012 4:41 PM
  • Or alternatively:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String testWord = String.Format(@"+CMGL: 0,""REC UNREAD"",6,27,""+971506178629"",145,""12/01/02,05:53:52-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"23:10:49-20"",0""{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 1,""REC UNREAD"",6,47,""+971506178649"",145,""12/01/02,05:55:11-20"",""12/01/03,{0}", Environment.NewLine);
                testWord += String.Format(@"03:51:30-20"",0{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 2,""REC UNREAD"",6,76,""+971506178677"",145,""12/01/02,06:01:05-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"23:46:46-20"",0{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 3,""REC UNREAD"",2,98,""+971506186817"",145,""12/01/01,16:51:57-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"16:51:58-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 4,""REC UNREAD"",2,99,""+971506186818"",145,""12/01/01,16:52:01-20"",""12/01/02,{0}", Environment.NewLine);
                testWord += String.Format(@"16:52:01-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 5,""REC UNREAD"",2,123,""+971506186842"",145,""12/01/01,16:54:09-20"",""12/01/02{0}", Environment.NewLine);
                testWord += String.Format(@",16:54:10-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 6,""REC UNREAD"",2,143,""+971506186861"",145,""12/01/01,16:57:27-20"",""12/01/02{0}", Environment.NewLine);
                testWord += String.Format(@",16:57:28-20"",70{0}", Environment.NewLine);
                testWord += String.Format(@"+CMGL: 7,""REC UNREAD"",6,150,""+971506186868"",145,""12/01/01,16:57:55-20"",""12/01/02{0}", Environment.NewLine);
                testWord += @",16:57:56-20"",70";
    
                String pattern = @"(?<a>\+CMGL):[ ](\d*),\""
                                 (?<b>[^\""]*)"",
                                 (?<c>\d*),(?<d>\d*),\""
                                 (?<e>\+\d*)"",(?<f>\d*),\""
                                 (?<g>\d*\/\d*\/\d*,\d{2}:\d{2}:\d{2}\-\d{2})\"",\""
                                 (?<h>\d{2}\/\d{2}\/\d{2}),?\r\n,?
                                 (?<i>\d{2}:\d{2}:\d{2}\-\d{2})\"",
                                 (?<j>\d*)";
    
                Regex rx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
                Match m = rx.Match(testWord);
                while (m.Success)
                {
                    Console.WriteLine(m.Groups["a"].Value);
                    Console.WriteLine(m.Groups["b"].Value);
                    Console.WriteLine(m.Groups["c"].Value);
                    Console.WriteLine(m.Groups["d"].Value);
                    Console.WriteLine(m.Groups["e"].Value);
                    Console.WriteLine(m.Groups["f"].Value);
                    Console.WriteLine(m.Groups["g"].Value);
                    Console.WriteLine(m.Groups["h"].Value);
                    Console.WriteLine(m.Groups["i"].Value);
                    Console.WriteLine(m.Groups["j"].Value);
                    Console.WriteLine("");
                    m = m.NextMatch();
                }
                Console.ReadLine();
            }
        }
    }
    
    



    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    Tuesday, January 03, 2012 6:17 PM
  • thaaaaaaaaaaaaaaaaanks very much

    it works finally :D

    Tuesday, January 03, 2012 7:58 PM