0

i use Regex extract string text here ..

+CMGR: "REC UNREAD","MSG","","2013/11/04 14:17:43+28" 0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E19

i need select text

"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E19"

i use code :

        string strRegex = @"\b+CMGR: w+\n(\w+)\b";
        RegexOptions myRegexOptions = RegexOptions.Multiline;
        Regex myRegex = new Regex(strRegex, myRegexOptions);
        string strTargetString = @"+CMGR: ""REC UNREAD"",""MSG"","""",""2013/11/04 13:52:18+28""" + "\r\n" + @"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197";

        foreach (Match myMatch in myRegex.Matches(strTargetString))
        {
            if (myMatch.Success)
            {
               return myRegex.Split(strTargetString);
            }
        }

is can't extract it..

Thank you

5
  • Where does from comes at the end in your result string? Your original string doesn't have it. Commented Nov 4, 2013 at 8:00
  • 3
    Try something like "\w+$" lol Commented Nov 4, 2013 at 8:02
  • @FLCL Was my thinking as well. This seems a little overkill for something which seems very simple. Commented Nov 4, 2013 at 8:04
  • May it be better to use something like strTargetString.Split('\n')[1] Commented Nov 4, 2013 at 8:08
  • Why don't you select only line 2? If you have multiple entries like the above, maybe it makes sense go through the input line by line. Commented Nov 4, 2013 at 8:10

2 Answers 2

3

To find last line you may use \w+$ pattern:

string strRegex = @"\w+$";    
Regex myRegex = new Regex(strRegex);
string strTargetString = @"+CMGR: ""REC UNREAD"",""MSG"","""",""2013/11/04 13:52:18+28""" + "\r\n" + @"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197";
return myRegex.Match(strTargetString);

. But the simplest way is to split string into lines and select the second one:

strTargetString.Split('\n')[1]
Sign up to request clarification or add additional context in comments.

Comments

0

Your code will work with this patter: "^+.+\s"

void Main()
{
    string strRegex = @"^\+.+\s";
        RegexOptions myRegexOptions = RegexOptions.Multiline;
        Regex myRegex = new Regex(strRegex, myRegexOptions);
        string strTargetString = @"+CMGR: ""REC UNREAD"",""MSG"","""",""2013/11/04 13:52:18+28""" + "\r\n" + @"0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197";

        foreach (Match myMatch in myRegex.Matches(strTargetString))
        {
            if (myMatch.Success)
            {
               Console.WriteLine(myRegex.Split(strTargetString));
            }
        }
}

output

0E2A0E270E310E2A0E140E350E040E230E310E1A00200E220E340E190E140E350E150E490E2D0E190E230E310E1A0E040E230E310E1A0E170E380E010E460E040E197

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.