使用正则表达式匹配文本中的模式

时间:2016-12-07 18:56:27

标签: php regex preg-match preg-match-all

使用正则表达式匹配最后一次出现的模式

在以下文字中:

commit 5a254a0bfc0232bd838157cada953f87a7b6ffad
Merge: 2b9fcd8 a9b214e
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 11:54:08 2016 -0500

    Merge branch 'master' of github.com:peterpan/testing

commit 2b9fcd8c10ee18ad2ea88190a3b10cc8affd4093
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 11:53:51 2016 -0500

    KWP245

commit a9b214eb476e3d1f3896db31125d6e8108dca06d
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 10:52:40 2016 -0600

    QZN0O5

commit a6be0abd17d3d6b1fdfe0711fe8e41a28b092212
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 10:52:38 2016 -0600

    QZN0O5

[编辑1]

我认为以下文字更为通用:

commit 5a254a0bfc0232bd838157cada953f87a7b6ffad
Merge: 2b9fcd8 a9b214e
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 11:54:08 2016 -0500

    Merge branch 'master' of github.com:peterpan/testing

commit 2b9fcd8c10ee18ad2ea88190a3b10cc8affd4093
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 11:53:51 2016 -0500

    KWP245

commit a9b214eb476e3d1f3896db31125d6e8108dca06d
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 10:52:40 2016 -0600

    QZN0O5

commit a6be0abd17d3d6b1fdfe0711fe8e41a28b092212
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 10:52:38 2016 -0600

    QZN0O5

commit bb2762bd17d3d6b1fdfe0711fe8e41a28b092212
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 10:52:38 2016 -0600

    H82LKJ

commit aa72dfeb476e3d1f3896db31125d6e8108dca06d
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 10:52:40 2016 -0600

我想从提交词中用“QZN0O5”捕获最后一段文本。

commit a9b214eb476e3d1f3896db31125d6e8108dca06d
Author: Peter Pan <peterpan@gmail.com>
Date:   Wed Dec 7 10:52:40 2016 -0600

    QZN0O5

我的意思是第一次提交文本QZN0O5

我试过了:

preg_match("/(?<!commit).*?QZN0O5/is", $text, $matches);

但没有成功。

  

实际上我想捕获提交代码:a9b214eb476e3d1f3896db31125d6e8108dca06d更具体。如果你有解决方案,那就更好了。

有关如何匹配该文字的任何想法吗?

1 个答案:

答案 0 :(得分:1)

您可以将以下正则表达式与preg_match_all一起使用并获取最后一项:

/^\s*QZN0O5\s*(.*\R.*\R.*+)(?!(?s:.*^\s*QZN0O5))/m

或者 - 或者:

/^\h*QZN0O5\s*(.*+(?:\R(?!\R).*)*+)(?![\s\S]*?^\h*QZN0O5$)/m

请参阅regex demoregex demo 2

<强>详情:

  • ^ - line start
  • \s* - 0个或更多空格
  • QZN0O5 - 您需要找到的代码
  • \s* - 0+ whitespaces
  • (.*\R.*\R.*+) - 3行......
  • (?!(?s:.*^\s*QZN0O5)) - 但未跟随任何0+字符(内联修饰符组(?s:....)使.匹配换行符),然后是行开头,0 +空格和你需要匹配的代码)
  • $ - 行尾

第二个正则表达式是相似的,但(.*+(?:\R(?!\R).*)*+)部分只是使正则表达式匹配,直到双线断开。

请参阅PHP demo

$re = '/^\h*QZN0O5\s*(.*+(?:\R(?!\R).*)*+)(?![\s\S]*?^\h*QZN0O5$)/m';
$str = "commit 5a254a0bfc0232bd838157cada953f87a7b6ffad\r\nMerge: 2b9fcd8 a9b214e\r\nAuthor: Peter Pan <peterpan@gmail.com>\r\nDate:   Wed Dec 7 11:54:08 2016 -0500\r\n\r\n    Merge branch 'master' of github.com:peterpan/testing\r\n\r\ncommit 2b9fcd8c10ee18ad2ea88190a3b10cc8affd4093\r\nAuthor: Peter Pan <peterpan@gmail.com>\r\nDate:   Wed Dec 7 11:53:51 2016 -0500\r\n\r\n    KWP245\r\n\r\ncommit a9b214eb476e3d1f3896db31125d6e8108dca06d\r\nAuthor: Peter Pan <peterpan@gmail.com>\r\nDate:   Wed Dec 7 10:52:40 2016 -0600\r\n\r\n    QZN0O5\r\n\r\ncommit a6be0abd17d3d6b1fdfe0711fe8e41a28b092212\r\nAuthor: Peter Pan <peterpan@gmail.com>\r\nDate:   Wed Dec 7 10:52:38 2016 -0600\r\n\r\n    QZN0O5\r\n\r\ncommit bb2762bd17d3d6b1fdfe0711fe8e41a28b092212\r\nAuthor: Peter Pan <peterpan@gmail.com>\r\nDate:   Wed Dec 7 10:52:38 2016 -0600\r\n\r\n    H82LKJ\r\n\r\ncommit aa72dfeb476e3d1f3896db31125d6e8108dca06d\r\nAuthor: Peter Pan <peterpan@gmail.com>\r\nDate:   Wed Dec 7 10:52:40 2016 -0600";
preg_match_all($re, $str, $matches);
echo end($matches[1]);