2016-10-04 46 views
0

嗨,我有以下文字:正则表达式来提取两个特定前锋之间的串斜线

file:/home/dx/reader/validation-garage/IDON/[email protected]#/ 

我需要检索从上面的字符串[email protected]#。如果我还可以更好地排除散列。

我试过寻找像这样的例子Regex to find text between second and third slashes,但无法让它工作,任何人都可以帮忙吗?

我使用PHP正则表达式来做到这一点。

+0

你的意思是有一个@'在它或路径或者什么的只是最后一部分'任何部分? – revo

+0

[^ \ /] + \/$这会给你[email protected]#/ –

回答

1

你可以试试下面

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

工作示例正则表达式是在这里:https://www.regex101.com/r/RYsh7H/1

说明:

[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:如果你真的不需要#你不必使用正则表达式。你可以考虑使用PHP的parse_url方法。

希望这有助于;

+0

这工作完美,谢谢! – olliejjc16

+0

不客气@ olliejjc16 –

0

没有正则表达式,你可以这样做:

$url_parts = parse_url('file:/home/dx/reader/validation-garage/IDON/[email protected]#/'); 
echo end(explode('/', $url_parts['path'])); 

或更好:

$url_path = parse_url('file:/home/dx/reader/validation-garage/IDON/[email protected]#/', PHP_URL_PATH); 
echo end(explode('/', $url_path)); 
0

basename()也适用,所以你也可以这样做:

echo basename('file:/home/dx/reader/validation-garage/IDON/[email protected]#/'); 
相关问题