我对目录中的大量文件进行循环,并希望提取文件名中以lin64exe
开头的所有数值,例如,lin64exe005458002.17
将匹配005458002.17
。我已经对这部分进行了排序,但是目录中还有其他文件,比如part005458
等。我怎样才能使它只得到数字(和。)在lin64exe
之后?
这是我目前为止写的:
[^lin64exe][^OTHERTHINGSHERE$][0-9]+
Regex与lin64exe
后面的小数点相匹配,
^lin64exe'K'd+'.'d+$
演示<?php
$mystring = "lin64exe005458002.17";
$regex = '~^lin64exe'K'd+'.'d+$~';
if (preg_match($regex, $mystring, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> 005458002.17
你也可以试试look around
(?<=^lin64exe)'d+('.'d+)?$
这里是demo
模式说明:
(?<= look behind to see if there is:
^ the beginning of the string
lin64exe 'lin64exe'
) end of look-behind
'd+ digits (0-9) (1 or more times (most possible))
( group and capture to '1 (optional):
'. '.'
'd+ digits (0-9) (1 or more times (most possible))
)? end of '1
$ the end of the string
注意:使用i
来忽略大小写
示例代码:
$re = "/(?<=^lin64exe)''d+(''.''d+)?$/i";
$str = "lin64exe005458002.17'nlin64exe005458002'npart005458";
preg_match_all($re, $str, $matches);
您可以使用这个正则表达式并使用捕获的组#1作为您的号码:
^lin64exe'D*(['d.]+)$
<<h3> RegEx演示/h3>代码:
$re = '/^lin64exe'D*(['d.]+)$/i';
$str = "lin64exe005458002.17'npart005458";
if ( preg_match($re, $str, $m) )
var_dump ($m[1]);