这是模式:
string str =
"+++++tom cruise 9:44AM something text here \r\n +++++mark taylor 9:21PM";
只有以or开头+++++
和结尾的字符串才会被选中。什么是 Regex.split 或 linq 查询模式?AM
PM
Talon 几乎得到它,但你需要一个最小的捕获,而不是贪婪。尝试
[+]{5}.*?(A|P)M
试试这个正则表达式:
@"[+]{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();
输出:
+++++tom cruise 9:44AM
或者:
@"[+]{5}\D+\d{1,2}:\d{1,2}[AP]M
我推荐这个正则表达式。它将匹配直到找到格式 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);
输出:
+++++tom cruise 9:44AM
+++++mark taylor 9:21PM
正则表达式将是:
[+]{5}.*AM|[+]{5}.*PM
你可以在这里试试:http ://regexpal.com/
它的第一个捕获是:
+++++tom cruise 9:44AM
第二个是
+++++mark taylor 9:21PM
用这个:
bool bResult = false;
String strInput = @"+++++tom cruise 9:44AM something text here \r\n +++++mark taylor 9:21PM";
foreach (string s in strInput.Split(new[]{'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries))
{
bResult |= Regex.IsMatch(s, @"^[+]+.+[AP]M$");
}
或获取结果使用:
var listResult = new List<string>();
String strInput = @"+++++tom cruise 9:44AM something text here \r\n +++++mark taylor 9:21PM";
foreach (string s in strInput.Split(new[]{'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries))
{
listResult.Add(Regex.Match(s, @"^[+]+(?<result>.+)[AP]M$").Groups["result"].Value);
}
这是根据您的需要搜索字符串的确切正则表达式代码
string str = "+++++tom cruise 9:44AM something text here \r\n +++++mark taylor 9:21PM asdasd";
var fileNames = from Match m in Regex.Matches(str, @"\++\++\++\++\++.+(PM|AM)")
select m.Value;
foreach (var s in fileNames)
{
Response.Write(s.ToString() + "\r\n");
}