2016-10-04 45 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]#/'); 
相關問題