Royi Namir
Royi Namir

Reputation: 148524

Regex - get the fileName?

I have this urls:

C:\Projects\Ensure_Solution\GD_EServices_Web\App_WebReferences\GD_Eservices_Web_Service\GD_Eservices_Web_Service.wsdl
C:\Projects\Ensure_Solution\GD_EServices_Web\App_WebReferences\GD_Eservices_Web_Service\GD_Eservices_Web_Service.wsdl

I want to get the wsdl file name ( no leading slash)

I have succeeded with 2 solutions :

\\[^\\]+$

\\(.(?!\\))+$

But this returns the leading slash : http://regexr.com?32lvi

enter image description here

how can I enhance my regex return only the file ?

Upvotes: 0

Views: 183

Answers (5)

xdazz
xdazz

Reputation: 160833

You just need to exclude the the leading slash in the regex.

var path = 'C:\\Projects\\Ensure_Solution\\GD_EServices_Web\\App_WebReferences\\GD_Eservices_Web_Service\\GD_Eservices_Web_Service.wsdl';
console.log(path.match(/[^\\]+$/));

And you could get it without regex, use split, and get the last element with pop:

console.log(path.split('\\').pop());

Upvotes: 1

Flow
Flow

Reputation: 35

Try this

\\[^\\]+$

Note:that means try with only one leading backslash

Upvotes: 0

0x41ndrea
0x41ndrea

Reputation: 385

Try with the negative look-ahead (?!\\)(.(?!\\))+$

Upvotes: 1

Tony T
Tony T

Reputation: 442

This should do it:

([\w\d_-]*)\.?[^\\\/]*$

This thread has some examples for javascript.

Alternatively, you can so a string split on "\" to create an array and get the last one in the array.

Upvotes: 0

Vyacheslav Voronchuk
Vyacheslav Voronchuk

Reputation: 2463

This should work [^\\]+$ But for your case I'd prefer smth like string.split('/').pop() (javascript) or array_pop(split('/', string)) (for php, I don't know language you are using) not regexp.

Upvotes: 1

Related Questions