Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find substrings with multiple newline characters in string using Regex

Tags:

c#

regex

I need to use regular expression to this string:


"\r\n+CMGL: 0,\"REC READ\",\"+79119968091\",,\"13/10/28,15:22:36+16\"\r\n10160008\r\n$GPMC,081915,A,5614.6801,N,04359.0266,E,0.0,190.2,121109,,*19\r\n+CMGL: 1,\"REC READ\",\"+79119968091\",,\"13/10/28,15:34:26+16\"\r\n#ALARM! \r\n10160008\r\n$GPMC,081915,A,5614.6801,N,04359.0266,E,0.0,190.2,121109,,*19\r\n\r\nOK\r\n"


I need to get 2 substrings(every substring starts with \r\n+CMGL: and ends with \r\n) from string above:

  1. \r\n+CMGL: 0,\"REC READ\",\"+79119968091\",,\"13/10/28,15:22:36+16\"\r\n10160008\r\n$GPMC,081915,A,5614.6801,N,04359.0266,E,0.0,190.2,121109,,*19\r\n
  2. \r\n+CMGL: 1,\"REC READ\",\"+79119968091\",,\"13/10/28,15:34:26+16\"\r\n#ALARM! \r\n10160008\r\n$GPMC,081915,A,5614.6801,N,04359.0266,E,0.0,190.2,121109,,*19\r\n

I have this regular expression

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

This regular expression works good but not for last group (\r\n((.|\n)*)\r\n\b). Problem is that i don't know how much \r\n symbols string will have until I see start of new substring (\r\n+CMGL: ). Thank in advance

like image 458
Alekstim Avatar asked Nov 01 '22 11:11

Alekstim


1 Answers

For Starting with \r\n+CMGL: and ending with \r\n you can use below regular expression :

\\r\\n\+CMGL:.*?\*19\\r\\n
like image 65
SpiderCode Avatar answered Nov 08 '22 11:11

SpiderCode