shindigwagon
shindigwagon

Reputation: 45

Regex - Extracting File Paths

I need to be able to extract the full file path out of this string (without whatever is after the file extension):

$/FilePath/FilePath/KeepsGoing/Folder/Script.sql (CS: 123456)

A simple solution such as the following could would work for this case, however it is only limited to a file extension with 3 characters:

(\$.*\..{3})

However, I find problems with this when the file contains multiple dots:

$/FilePath/FilePath/File.Setup.Task.exe.config (CS: 123456)

I need to be able to capture the full file path (from $ to the end of whatever the file extension is, which can be any number of things). I need to be able to get this no matter how many dots are in the name of the file. In some cases there are spaces in the name of the file too, so I need to be able to incorporate that.

Edit: The ending (CS....) in this case is not standard. All kinds of stuff can follow the path so I cannot predict what will come after the path, but the path will always be first. Sometimes spaces do exist in the file name.

Any suggestions?

Upvotes: 0

Views: 272

Answers (1)

Nir Levy
Nir Levy

Reputation: 4740

Try this:

(\$.*\.[\w.-]+)

But! it will not properly match files with space or special chars in the file extension. If you need to match files that might have special chars in the file extension you'll need to elaborate on the input (is it quoted? is it escaped?).

Upvotes: 1

Related Questions