简体   繁体   English

php Regex文件路径|特定字符后的第二个匹配

[英]php Regex file path | second matching after specific character

I trying to extract file patches, without disk letter, that are inside text. 我试图提取文本内部没有磁盘字母的文件补丁。 Like from AvastSecureBrowserElevationService; C:\\Program Files (x86)\\AVAST Software\\Browser\\Application\\elevation_service.exe [X] 喜欢来自AvastSecureBrowserElevationService; C:\\Program Files (x86)\\AVAST Software\\Browser\\Application\\elevation_service.exe [X] AvastSecureBrowserElevationService; C:\\Program Files (x86)\\AVAST Software\\Browser\\Application\\elevation_service.exe [X] extract :\\Program Files (x86)\\AVAST Software\\Browser\\Application\\elevation_service.exe . AvastSecureBrowserElevationService; C:\\Program Files (x86)\\AVAST Software\\Browser\\Application\\elevation_service.exe [X] extract :\\Program Files (x86)\\AVAST Software\\Browser\\Application\\elevation_service.exe

My actual regex look like this, but it will stop on any space, which can contains file names. 我的实际正则表达式看起来像这样,但它会在任何可能包含文件名的空间上停止。

(?<=:\\)([^ ]*)

The soulution that I figure out is, that I can match first space character after dot, because there is very little chance that there will be some directory name with space after dot, and I will always do fast manual check. 我弄清楚的是,我可以匹配点之后的第一个空格字符,因为在点之后会有一些带空格的目录名的可能性很小,我总是会做快速的手动检查。 But I do not know how to write this in regex 但我不知道如何在正则表达式中写这个

You may use this regex for this purpose: 您可以将此正则表达式用于此目的:

(?<=[a-zA-Z]):[^.]+\.\S+

RegEx Demo RegEx演示

RegEx Details: RegEx详细信息:

  • (?<=[a-zA-Z]) : Lookbehind to assert we have a English letter before : (?<=[a-zA-Z]) :断言断言我们之前有一封英文字母:
  • : : Match literal : : :匹配文字:
  • [^.]+ : Match 1+ non-dot characters [^.]+ :匹配1个非点字符
  • \\. : Match literal . :匹配文字.
  • \\S+ : Match 1+ non-whitespace characters \\S+ :匹配1个非空白字符

Here we would consume our entire string, as we collect what we wish to output, and we would preg_replace : 在这里我们将使用整个字符串,因为我们收集了我们希望输出的内容,并且我们将preg_replace

.+C(:\\.+\..+?)\s.+

Test 测试

$re = '/.+C(:\\.+\..+?)\s.+/m';
$str = 'AvastSecureBrowserElevationService; C:\\Program Files (x86)\\AVAST Software\\Browser\\Application\\elevation_service.exe [X]';
$subst = '$1';

$result = preg_replace($re, $subst, $str);

echo $result;

Demo 演示

You can use the following regex: 您可以使用以下正则表达式:

[A-Z]\K:.+\.\w+

It will match any capital letter followed by : , then any character string ending wit . 它将匹配任何大写字母后跟: ,然后任何以wit结尾的字符串. , followed by at least one word character. ,后跟至少一个单词字符。

\\K removes from the match what comes before it. \\K从比赛中删除之前的内容。

Demo 演示

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

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