Php regex,找到关键字时获取行后的第一个字符
我一直在与正则表达式构建器和教程混搭,但我无法理解这一点。我有一个关键字,我可以找到,我需要找到同一行或下一行的时间戳 我的文本看起来像这样Php regex,找到关键字时获取行后的第一个字符,php,regex,preg-match,Php,Regex,Preg Match,我一直在与正则表达式构建器和教程混搭,但我无法理解这一点。我有一个关键字,我可以找到,我需要找到同一行或下一行的时间戳 我的文本看起来像这样 16:51:35.536 This is a bunch of completely random text although it has a keyword. This keyword is unique 16:51:35.806 This is another line which has all sorts of crap i dont reall
16:51:35.536 This is a bunch of completely random text although it has a keyword. This keyword is unique
16:51:35.806 This is another line which has all sorts of crap i dont really care about.
it has a keyword (.*) ([0-9){2}:[0-9){2}:[0-9){2})
现在我要捕捉的是获取时间戳的前8个字符,即16:51:35
所以我试着做这样的事情
16:51:35.536 This is a bunch of completely random text although it has a keyword. This keyword is unique
16:51:35.806 This is another line which has all sorts of crap i dont really care about.
it has a keyword (.*) ([0-9){2}:[0-9){2}:[0-9){2})
但不管出于什么原因,即使我在末尾添加/m或/s或/s,它也会卡在该行上
这有助于进入下一行:
([\s\S]*)
所以我试过了
it has a keyword ([\s\S]*)(?=[0-9){2}:[0-9){2}:[0-9){2}) but that didnt work either
我很确定这比我做的要简单,任何想法如果你想要一行的前八个字符带有关键字字符串“foobar”,那么你可以使用:
^(.{8})[^\n]*foobar.*?$
您可以看到,这项工作很容易修改,以获得下一行的前八个字符,或者对前八个字符进行限制。首先,您的意思是
[0-9)
而不是[0-9]
?