从图案中提取文本

时间:2019-10-12 14:00:03

标签: c# regex

我有一个sim 800模块。我阅读了存储在SIM卡中的所有消息。那传来波纹管的反应。我想从响应中分离出电话号码,时间和文字。但是我不知道怎么办


+CMGL: 1,"REC READ","number","","2019/10/11 17:09:44+14"
this is text

+CMGL: 2,"REC READ","989149161278","","2019/10/11 17:38:18+14"
this is text

+CMGL: 3,"REC READ","989149161278","","2019/10/11 17:55:03+14"
this is text

+CMGL: 4,"REC READ","989149161278","","2019/10/11 17:57:09+14"
this is text

+CMGL: 5,"REC READ","989149161278","","2019/10/11 18:14:22+14"
this is text

+CMGL: 13,"REC READ","989149161278","","2019/10/11 19:04:40+14"
this is text

+CMGL: 14,"REC READ","989149161278","","2019/10/11 19:07:12+14"
this is text

+CMGL: 15,"REC READ","989149161278","","2019/10/11 19:08:23+14"
this is text

+CMGL: 16,"REC READ","989149161278","","2019/10/11 19:21:17+14"
this is text

+CMGL: 17,"REC READ","989149161278","","2019/10/11 19:30:31+14"
this is text

+CMGL: 18,"REC READ","989149161278","","2019/10/11 20:15:49+14"
this is text

+CMGL: 19,"REC READ","989149161278","","2019/10/11 20:41:36+14"
this is text

+CMGL: 20,"REC READ","number","","2019/10/11 20:41:41+14"
this is text

OK

2 个答案:

答案 0 :(得分:2)

这可能不是最好的方法,但是考虑到您的响应结构将始终保持不变,请考虑:

\+CMGL:\s\d+\,\"REC\sREAD\",\"(?<phone>.+)\",\"(?:.+)?\",\"(?<time>.+)\"|^(?<message>.+)$

Regex Demo

本质上,捕获整个字符串并通过组提取必要的信息。您可以通过组名访问各个组(请参见右侧的链接)。

答案 1 :(得分:0)

您应该对此进行研究以解决您的问题。 https://www.tutorialspoint.com/csharp/csharp_regular_expressions.htm

或者,您也可以使用字符串拆分方法在逗号之间划分内容。 https://www.geeksforgeeks.org/string-split-method-in-c-sharp-with-examples/