PHP regex在第二次出现字符时开始


php regex start at the second occurrence of a character?

我一直在与这个战斗了一段时间,我有这个字符串:storage/12426--the sunflower boys--07-09-2014/Authorization letter/Authorization letter--4--09-15-2015--15-39.pdf

,我希望只得到位于前两个斜杠之间的这部分07-09-2014

到目前为止,我想出了这个:--[^/]*,只给我--the sunflower boys--07-09-2014

我怎样才能得到我想要的?

使用数字'd match:

/^[^/]*'/[^/]*('d'd-'d'd-'d'd'd'd)'//

您可以在2个否定字符类中使用捕获组:

'/[^'/]*('d{2}-'d{2}-'d{4})[^'/]*'/

参见演示https://regex101.com/r/mL3aF7/1

如果格式一致,您可以在/上分裂并取第二组,然后在--上分裂并取第三组。

$date = explode('--', explode('/', $str)[1])[2];

您可以使用匹配重置'K基于regex:

--[^-]*--'K[^/]*

RegEx演示