Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to find last folder in a path

I would like to find the last folder in a path. Ex:

G:\DATA1\DATA2\DATA3\DATA4\my target\file.xxx

I need to extract my target. I tried (^.*\\)(.*\\) but the result is my target\. Someone can fix it?

like image 242
gerry_76 Avatar asked Oct 19 '25 03:10

gerry_76


1 Answers

Use the power of greedy operator along with capturing group.

.*\\([^\\]+)\\

OR

Use the same regex but get out of the last backslash from the second capturing group.

.*\\(.*)\\

Get the string you want from group index 1.

DEMO

like image 187
Avinash Raj Avatar answered Oct 22 '25 05:10

Avinash Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!