0

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?

9
  • 1
    Is the ending predictable? Can't you just trim out " (CS....)"? Commented Jun 20, 2018 at 13:22
  • Will the file always end with something along the lines of ` (CS: ....)`? If so, you could grab everything up until that... Commented Jun 20, 2018 at 13:22
  • Use context Regex.Match(s, @"(\$/.*) \(CS: \d+\)").Groups[1].Value Commented Jun 20, 2018 at 13:24
  • @BrootsWaymb ending is not very predictable. It just happens to be (CS....) in this case but sometimes there are other things as well, which all don't follow a standard format so they could really be anything. Commented Jun 20, 2018 at 13:28
  • @shindigwagon will the path contain spaces? - if not path.Split().First(); Commented Jun 20, 2018 at 13:29

1 Answer 1

1

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?).

Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm. Spaces are not a common occurrence, but they do happen from time to time. This seems like it works otherwise though. Thanks :)
If you could provide a larger dataset we can try to find a better solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.