Y
Y
Yuriy2020-07-10 14:59:32
Regular Expressions
Yuriy, 2020-07-10 14:59:32

How to get the last directory in a line?

You need to get the last directory from the link (it doesn’t matter if it’s a link or just a string)

Example:

https://site.ru/catalog/cat1/ you need to get cat1
https://site.ru/catalog/cat1/cat2/ you need to get cat2
https ://site.ru/catalog/cat1/cat2/cat3/ you need to get cat3
https://site.ru/catalog/cat1/cat2/cat3/cat4/ you need to get cat4

This means that the nesting is different, it can be like 1, and 10+, but you need to pull out the last one.

/(?:.(?!/))+$ everything works after the last one/

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2020-07-10
@yous

[^\/]+(?=\/$)

S
Stalker_RED, 2020-07-10
@Stalker_RED

([^\/]+)\/$

S
SOTVM, 2020-07-11
@sotvm

~ $ basename https://site.ru/catalog/cat1/cat2/cat3/cat4/
cat4

variant for javascript

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question