Solved

How to extract a path from a given string with REGEX in C#

Posted on 2014-10-29
8
272 Views
Last Modified: 2014-10-30
Hi,

I would like to extract a path from any given string with a regex.
We can have multiple scenario like :

hi my name is G:\Assets\Resources\Firstname.png say hello!
G:\Assets\Resources\Firstname.png say hello!
Hi my name is G:\Assets\Resources\Firstname.png
G:\Assets\Resources\Firstname.png

In all cases, the result would be "G:\Assets\Resources\Firstname.png"

Can you help me to do that?


Thanks in advance,

Kind Regards.
0
Comment
Question by:Dnx_7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 40410317
this should do:
(.\:\\[^ ]*)
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 40410318
I am testing here:
http://regex101.com/
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 40410321
I'll need to have a closer look at that, but in the meanwhile I'd like to recommend the free tool program Expresso (d/l from here). It's a tool for designing and testing regular expressions.

I'll be back !
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 14

Expert Comment

by:frankhelk
ID: 40410351
After playing a bit, I came to this one ... worked well in Expresso in a text stream of your examples. Maybe the \r\n part isn't needed for singular one line strings outside of a longer text.

([a-zA-Z]\:\\[\w\\\.]*)[\r\n ]+?.*

BTW: That works only if the path doesn't contain blanks. If the path contains blanks, the task would be an impossible one if there are no definite delimiters.

@Guy Hengel: Your solution seems to omit that the first character in the path must be a letter (not a number or special character). Beside of that it gives some strange results in Expresso ...
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40410366
If you consider only absolute paths without spaces in it, you can use the following regex:
[a-z]:(\\[^"<>|\u0000-\u001F:*?\\/])*

Open in new window

0
 
LVL 2

Author Comment

by:Dnx_7
ID: 40411871
hi,
Thanks all for the help.

frankhelk, it seems that your solution is the closest one but indeed, we can have space in path BUT file extension can be specified like '.png' or '.bmp', can you adapt the regex for me? the regex is definitely not my friend lol

louisfr the regex seems not to be valid...

Thanks!
0
 
LVL 14

Accepted Solution

by:
frankhelk earned 300 total points
ID: 40412712
It's not a close friend of mine, either ... but Expresso is much of help.

How about this:
([a-zA-Z]\:\\[\w\\\. ]*\.(?:jpg|png|gif)).*

Open in new window


The crux with that one is that you have to know the extension in advance ...

Another weak spot you should possibly edit is the [\w\\\. ] part. It limits the charaters between the X:\ and the .ext part to alphanumeric characters, underscore, dots, and backslashes. Maybe that has to be extended a bit depending on language ...
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 100 total points
ID: 40412719
I started replying with the exclusion of spaces in mind, but I didn't include that in my regex (what do you think is not valid?)
[a-z]:(\\[^"<>|\u0000-\u001F:*?\\/])*
It allows anything starting with a drive letter up to the first forbidden character, including the drive letter alone.

The following ensures there is a filename with entension.
[a-z]:\\([^"<>|\u0000-\u001F:*?\\/]\\)*([^"<>|\u0000-\u001F:*?\\/])+\.([^"<>|\u0000-\u001F:*?\\/])+
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question