Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEX: Extract paths from string

Tags:

regex

I would like to extract paths in the form of:

$/Server/First Level Folder/Second_Level_Folder/My File.extension

The challenge here is that the paths are embedded in a "free form" email like so:

Hello,

 You can download the file here:
  • $/Server/First Level Folder/Second_Level_Folder/My File.extension <- Click me!

Given a string, I would like to extract all paths from it using RegEx. Is this even possible?

Thanks!

like image 770
Ian Avatar asked Sep 13 '25 10:09

Ian


1 Answers

Yes, this is possible (\$/.*?\.\S*) should do the job just fine.

\$/ matches the start of the path

.*? matches everything till the next part of the regex

\.\S* matches the dot and anything but a whitespace (space, tab)

And the ( ) around it make it capture all that is matched.

EDIT:

For further use

Just the path

(\$/.*?/)[^/]*?\.\S*

Just the filename

\$/.*?/([^/]*?\.\S*)

like image 62
B8vrede Avatar answered Sep 16 '25 07:09

B8vrede