You are viewing limited content. For full access, please sign in.

Question

Question

Pattern match folder name from path

asked on September 16, 2015

Hi,

 

I'm looking for a regular expression to extract the first folder name from a path. For example, from a path such as X:\ABC Limited\Correspondence\2015 I want only to extract "ABC Limited".

 

I've tried expressions along the lines of \\(.+)\\ but it matches the entire string due to the wildcard so need it to be more specific.

Can any regular expression gurus save me some time?!


 

0 0

Answer

SELECTED ANSWER
replied on September 16, 2015

Give this a go:

(?:\:\\)([^\\]*)(?:\\)

It should grab everything after a ":\" and stop before the next "\"

 

Cheers,

Carl

0 0

Replies

replied on September 16, 2015

.:\\([^\\]+) should do it. [^\\] means "anything but a backslash, so it will make the regular expression stop matching when it hits the next backslash rather than going all the way to the end.

2 0
replied on October 19, 2021

THANK YOU!!!!! You just solved a problem I have been fighting with for over 2 weeks. You're a lifesaver!!!!!

0 0
replied on September 16, 2015
(?:\:\\)(.*)(?:\\)

Give that a try.

 

This will begin grabbing everything after ":\" and stop at the next "\".

 

Cheers,

Carl

replied on September 16, 2015

Thanks both. The latter one seems to do the trick!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.