Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-29
8
Medium Priority
?
286 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 400 total points
ID: 40410317
this should do:
(.\:\\[^ ]*)
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1200 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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