Zack
Zack

Reputation: 2859

Regex to parse a string and match a URL or folder path

I'm trying to change a regex that will match a url like http://www.google.com, and also allow it to match a folder name such as j:\Folder\Name\Here

I'm parsing the text of a message for any links that may be present within, and creating a Process.Start(string) call with the matched string.

The regex I have now looks like this:

(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'"".,<>?«»“”‘’]))

I'm thought I could add to the /{1,3} part to match \{1,1} also and it might work, but that doesn't seem to be the case.

I'm not sure what else the regex is exactly doing because I did not write it myself.

Does anyone have a working example already of a regex that will match URLs as well as file system folder paths? Or is there some way to change this existing regex to work for that purpose?

Upvotes: 1

Views: 1572

Answers (1)

Sedecimdies
Sedecimdies

Reputation: 152

have your tried :

[^ ]+?:(//[^ ]*|\\.+\\[^ ]*)

it will match :

http://www.google.com

and

C:\windows\temp internetfiles\

in the string where http://www.google.com is the way to go if you want to save your file to C:\windows\temp internetfiles\ quick and easy

Upvotes: 2

Related Questions