Php 正则表达式在单词之间查找,但don';t返回搜索词

Php 正则表达式在单词之间查找,但don';t返回搜索词,php,regex,Php,Regex,我正试图从电子邮件中获取主题 这项工作: preg_match_all('/Subject:(.*?)Date:/', $theEmail, $subjects); 但结果是这样的: "Subject:This is my subject!Date:" 我只想这是我的主题

我正试图从电子邮件中获取主题

这项工作:

preg_match_all('/Subject:(.*?)Date:/', $theEmail, $subjects);
但结果是这样的:

"Subject:This is my subject!Date:"

我只想这是我的主题除了rock321987的评论之外,另一个解决方案是查看
像这样的断言


正则表达式:
(?尝试只输出捕获组
$subjects[1][0]
,即:

$theEmail = "Subject:This is my subject!Date:";
preg_match_all('/Subject:(.*?)Date:/', $theEmail, $subjects);
$theSubject = $subjects[1][0];
echo $theSubject;
//This is my subject!

演示


您可以使用
主题[1][0]
以如下方式访问值:

$theEmail = "Subject:This is my subject!Date:";
preg_match_all('/Subject:(.*?)Date:/', $theEmail, $subjects);

print_r($subjects[1][0]);


使用
preg\u match\u all
时,
$subjects
是一个数组,包含所有可能的匹配项,但第一个匹配项,即
$subjects[0][0]
始终是匹配的整个字符串,而不考虑任何捕获组

$subjects[1][0]
行得通吗?你能告诉我你的答案和我的答案有什么不同吗?是
打印出来的而不是
回音的吗?!@PedroLobito事实上我是在评论中写下答案的。所以我花了一些时间才在这里回答,而你抢先回答了我。我现在已经添加了一些解释
打印($subjects[1][0])
echo$subjects[1][0]
都返回空白哦!所以我有两个匹配项,第一个是“-$subjects[1][1]给了我想要的东西。@RandyHall你能解释一下你的问题吗?我很困惑
$theEmail = "Subject:This is my subject!Date:";
preg_match_all('/Subject:(.*?)Date:/', $theEmail, $subjects);

print_r($subjects[1][0]);