Solved

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

Posted on 2014-10-29
8
262 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
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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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