Solved

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

Posted on 2014-10-29
8
257 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 142

Assisted Solution

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

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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now