提取以c#开头和结束的字符串

use*_*952 5 c# regex string

这是模式:

string str =
   "+++++tom cruise 9:44AM something text here \r\n +++++mark taylor 9:21PM";
Run Code Online (Sandbox Code Playgroud)

只有以#开头+++++和结束AMPM应该被选中的字符串.什么是Regex.split或linq查询模式?

The*_*ask 3

试试这个正则表达式:

@"[+]{5}[^\n]+[AP]M"

var str = "+++++tom cruise 9:44AM something text here \r\n +++++mark taylor 9:21PM";
var match = Regex.Match(str, @"[+]{5}[^\n]+[AP]M").Captures[0];
match.Value.Dump(); 
Run Code Online (Sandbox Code Playgroud)

输出:

+++++tom cruise 9:44AM
Run Code Online (Sandbox Code Playgroud)

或者:

@"[+]{5}\D+\d{1,2}:\d{1,2}[AP]M
Run Code Online (Sandbox Code Playgroud)

我推荐这个正则表达式。它将匹配直到在 xY:xY:AM/PM 格式中找到一个小时,其中 Y 是可选的。试驾:

string str = "+++++tom cruise 9:44AM something text here \r\n +++++mark taylor 9:21PM";
foreach(Match match in Regex.Matches(str, @"[+]{5}\D+\d{1,2}:\d{1,2}[AP]M"))
        Console.WriteLine(match.Value);
Run Code Online (Sandbox Code Playgroud)

输出:

+++++tom cruise 9:44AM
+++++mark taylor 9:21PM
Run Code Online (Sandbox Code Playgroud)