Nigel Thomas
Nigel Thomas

Reputation: 1929

Regular expression to find path from filename

I need to replace the directory structure or the file-path from the file name with whitespace.

Input text :

D:\LIGHTNING_ProductSecurity\BACKEND\CORE\dal\src\main\java\com\adminserver\dal\AuthPlanDal.java

Output text :

D:\LIGHTNING_ProductSecurity\BACKEND\CORE\dal\src\main\java\com\adminserver\dal\

How can it be done?

Upvotes: 0

Views: 131

Answers (3)

manish
manish

Reputation: 1458

this might help:

string path = @"D:\LIGHTNING_ProductSecurity\BACKEND\CORE\dal\src\main\java\com\adminserver\dal\AuthPlanDal.java";
string fullPathWithoutFilename = Path.GetFullPath(path).Replace(Path.GetFileName(path), "");

Upvotes: 0

Nigel Thomas
Nigel Thomas

Reputation: 1929

(.*\\) 

Will return only the patch preceding the file name.

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Use this regex for search, and replace with empty.

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

This one is matching the last portion(i.e. AuthPlanDal.java) after the \ from the file path.

Anyone can try this at online.

Upvotes: 0

Related Questions