简体   繁体   English

正则表达式PHP-不匹配特定的字符串,后跟数字

[英]Regex PHP - dont match specific string followed by numeric

Im looping over a large number of files in a directory, and want to extract all the numeric values in a filename where it starts lin64exe , for instance, lin64exe005458002.17 would match 005458002.17 . 我遍历了目录中的大量文件,并希望提取文件名中所有以lin64exe开头的数字值,例如lin64exe005458002.17将匹配005458002.17 I have this part sorted, but in the directory there are other files, such as part005458 and others. 我已经对这部分进行了排序,但是在目录中还有其他文件,例如part005458和其他文件。 How can I make it so I only get the numeric (and . ) after lin64exe ? 我怎样才能使它只在lin64exe之后得到数字(和。)?

This is what I have so far: 这是我到目前为止的内容:

[^lin64exe][^OTHERTHINGSHERE$][0-9]+

Regex to match the number with decimal point which was just after to lin64exe is, 正则表达式匹配到lin64exe之后的小数点后的lin64exe

^lin64exe\K\d+\.\d+$

DEMO 演示

<?php
$mystring = "lin64exe005458002.17";
$regex = '~^lin64exe\K\d+\.\d+$~';
if (preg_match($regex, $mystring, $m)) {
    $yourmatch = $m[0]; 
    echo $yourmatch;
    }
?> //=> 005458002.17

You can use this regex and use captured group #1 for your number: 您可以使用此正则表达式并将捕获的组#1用于您的号码:

^lin64exe\D*([\d.]+)$

RegEx Demo 正则演示

Code: 码:

$re = '/^lin64exe\D*([\d.]+)$/i'; 
$str = "lin64exe005458002.17\npart005458"; 

if ( preg_match($re, $str, $m) )
    var_dump ($m[1]);

You can try with look around as well 您也可以尝试环顾四周

(?<=^lin64exe)\d+(\.\d+)?$

Here is demo 这是演示

Pattern explanation: 模式说明:

  (?<=                     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

Note: use i for ignore case 注意:使用i忽略大小写

sample code: 样例代码:

$re = "/(?<=^lin64exe)\\d+(\\.\\d+)?$/i";
$str = "lin64exe005458002.17\nlin64exe005458002\npart005458";

preg_match_all($re, $str, $matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM