![](/img/trans.png)
[英]how to extract a certain digit from a String using regular expression in php?
[英]PHP regular expression to extract date from string
我对这个正则表达式一无所知。 我没有得到我需要的正则表达式。 这是我必须在PHP中使用正则表达式拆分的字符串格式。
ABCxxxx_ABCDEfghi_YYYYmmddhhmmss.mp4
在这个字符串中
ABC -word(case sensitive)
x -any digit
ABCDEfghi -word(case sensitive)
YYYYmmddhhmmss -timestamp value
.mp4 -word preceded with a dot(.)
我需要的只是从此字符串中提取日期。
即,将YYYYmmdd
用作变量。
我知道这不是写的方法,但是我尝试过。 这是尝试:
$s = "ABC0000_ABCDEfghi_20000101223344.mp4";
$regex = "/\ABC[0-9]{4}_ABCDEfghi_&var=[0-9]{8}[0-9]{6}+\.mp4/";
$matches = array();
$s = preg_match($regex, $s, $matches);
print_r($matches[1]);
错误:
(!)注意:未定义的偏移量:第6行的D:\\ wamp \\ www \\ Test \\ regex.php中的1调用堆栈时间存储器功能位置1 0.0000 241296 {main}().. \\ regex.php:0
我被困住了。 请帮我解决。
(?<=_)\d+(?=\.mp4)
只需使用此功能即可。
https://regex101.com/r/bW3aR1/4
$re = "/(?<=_)\\d+(?=\\.mp4)/";
$str = "ABC0000_ABCDEfghi_20000101223344.mp4";
preg_match_all($re, $str, $matches);
在A
之前删除\\
不要使用&var
,而是需要使用捕获组。
$regex = '~ABC[0-9]{4}_ABCDEfghi_([0-9]{8})[0-9]{6}\\.mp4~';
如有必要,添加开始^
和结束$
锚。
$s = "ABC0000_ABCDEfghi_20000101223344.mp4";
$regex = '~^ABC[0-9]{4}_ABCDEfghi_([0-9]{8})[0-9]{6}\.mp4$~';
preg_match($regex, $s, $matches);
print_r($matches[1]);
输出:
20000101
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.