简体   繁体   English

正则表达式提取两个特定的正斜杠之间的字符串

[英]Regex to extract a string between two specific forward slashes

Hi I have the following text: 嗨,我有以下文字:

file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/

I need to retrieve test-test-test@2016-10-04.txt# from the string above. 我需要从上面的字符串中检索test-test-test@2016-10-04.txt# If I can also exclude the hash even better. 如果我还可以更好地排除哈希。

I've tried looking at examples like this Regex to find text between second and third slashes but having trouble getting it working, can anyone help? 我尝试查看正则表达式之类的示例, 以在第二个和第三个斜杠之间查找文本,但是在使其无法正常工作的情况下,有人可以帮忙吗?

I'm using PHP regex to do this. 我正在使用PHP正则表达式来执行此操作。

You may try regex expression below 您可以在下面尝试正则表达式

\/([a-z\-]*\@[0-9\-\.]*[a-z]{3}\#)\/

A working example is here: https://www.regex101.com/r/RYsh7H/1 一个有效的示例在这里: https : //www.regex101.com/r/RYsh7H/1

Explanation: 说明:

[a-z\-]* => Matches test-test-test part with lowercase and can contain dahses
\@ => Matches constant @ sign
[0-9\-\.]* => Matches the file name with digits, dashes and {dot}
[a-z]{3}\# => Matches your 3 letter extension and #

PS: If you really do not need # you do not have to use regex. PS:如果您真的不需要#,则不必使用正则表达式。 And you may consider using parse_url method of PHP. 您可以考虑使用PHP的parse_url方法。

Hope this helps; 希望这可以帮助;

Without regex you can do: 没有正则表达式,您可以执行以下操作:

$url_parts = parse_url('file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/');
echo end(explode('/', $url_parts['path']));

or better: 或更好:

$url_path = parse_url('file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/', PHP_URL_PATH);
echo end(explode('/', $url_path));

basename()也可以,所以您也可以这样:

echo basename('file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/');

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

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