Solved

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

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

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 13

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 13

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

13 Experts available now in Live!

Get 1:1 Help Now