So che questo è un vecchia domanda, ma può essere utile per le persone che vedono questa domanda.
L'approccio migliore per ottenere l'estensione dal nome del file all'interno di un url, anche con i parametri sono con regex.
È possibile utilizzare questo schema (non solo gli URL):
.+(\.\w{3})\?*.*
Spiegazione:
.+ Match any character between one and infinite
(...) With this you create a group, after you can use for getting string inside the brackets
\. Match the character '.'
\w Matches any word character equal to [a-zA-Z0-9_]
\?* Match the character '?' between zero and infinite
.* Match any character between zero and infinite
Esempio:
http://example.com/file.png
http://example.com/file.png?foo=10
But if you have an url like this:
http://example.com/asd
This take '.com' as extension.
in modo da poter utilizzare un modello forte per gli URL come this:
.+\/{2}.+\/{1}.+(\.\w+)\?*.*
Spiegazione:
.+ Match any character between one and infinite
\/{2} Match two '/' characters
.+ Match any character between one and infinite
\/{1} Match one '/' character
.+ Match any character between one and infinite
(\.\w+) Group and match '.' character and any word character equal to [a-zA-Z0-9_] from one to infinite
\?* Match the character '?' between zero and infinite
.* Match any character between zero and infinite
Esempio:
http://example.com/file.png (Match .png)
https://example.com/file.png?foo=10 (Match .png)
http://example.com/asd (No match)
C:\Foo\file.png (No match, only urls!)
http://example.com/file.png
http: .+
// \/{2}
example.com .+
/ \/{1}
file .+
.png (\.\w+)
Bye
Si potrebbe cercare di ottenere ultima posizione di?. Se trovato, trova l'ultima posizione di. prima di ciò e restituire tutto in mezzo. Se no? viene trovato, qualunque cosa venga dopo l'ultima posizione di. sarà la tua estensione di file. – Crono
La sottostringa dovrebbe funzionare, assicurati di avere un account per le estensioni con una lunghezza maggiore di 3. – Neolisk
sei tu quello che fornisce gli URL? Sono sul tuo sito o su siti di terze parti? –