RegexHelp
RegexHelp

Reputation: 47

Regex not matching the content between the new lines (\r\n)

I'm using Regex for the first time, so I'm sorry if this is a basic question..

I'm trying to match this input string with regex:

   string input = "\r\n+CMGL: 3,\"REC UNREAD\",\"+999999999\",,\"21/07/15,14:06:38+08\",145,10\r\nTest1\nTest2\r\n\r\nOK\r\n";
    
   Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",,""(.+),(.+)""\r\n(.+)\r\n");

I have no problem matching the first part of the input with this code:

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

But I cannot find the way to match this last part: \r\nTest1\nTest2\r\n

Really appreciate the help.

Upvotes: 3

Views: 156

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

Your current problem is that you forgot to consume the ,145,10 text on the line with double quotes, you are trying to match the line break immediately after the last ".

You can consume them with .* and add another (.*) to match the Test2 line:

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

See the regex demo. See the Table tab:

enter image description here

Upvotes: 2

Related Questions