0

I have this text:

Unexpected error creating debug information file 'c:\Users\Path1\Path2\Strategies\Path3\CustomStrategy.PDB' -- 'c:\Users\Path1\Path2\Strategies\Path3\CustomStrategy.pdb: The system cannot find the path specified.

I need to parse out the file paths c:\Users\Path1\Path2\Strategies\Path3 or c:\Users\Path1\Path2\Strategies\Path3\CustomStrategy.PDB, whatever is easier. I tried to use the following Regex

\w:.+[.]\w{3}

But, this RegEx doesn't stop at first file extension and continues to match the the second instance of the path, stopping at the second instance of .pdb; thus putting both file paths in one regex match.

What do I need to change in order for the regex to parse the two paths as two separate matches? Thanks.

1
  • can you provide more information for what language you are using? path parsing with regex is complicated. .net for example has the static method Path.GetDirectoryName(string path) which returns the path. use framework methods when possible. there can be spaces in the path, unicode characters, all kind of special characters. Commented Aug 8, 2012 at 11:16

3 Answers 3

2

Non-greedy re:

\w:.+?[.]\w{3}

Note ? after +.

Also, if your path contains no dots except the last one, you can write it so:

\w:[^.]+[.]\w{3}

If you are not sure that the extension consists of three letters, you must specify the range:

\w:[^.]+[.]\w{1,3}

And when you are not sure that your path has extension at all, but it contains no spaces, then:

\w:\S+
Sign up to request clarification or add additional context in comments.

2 Comments

and extension will always exist and consist of 3 letters?
Well, that's my hope. Otherwise, not sure how to match the path. Do you know of a path matcher that may work here?
2

What about this

\w:\\(?:[^\\\s]+\\)+

See it here on Regexr

\w:\\ matches a word character, a : and a backslash

(?:[^\\\s]+\\)+ matches the directories, non-backslash or non whitespace characters till a backslash, and this repeated.

So, this would match both paths c:\Users\Path1\Path2\Strategies\Path3. works as long as the directory names does not contain spaces.

1 Comment

I like the first regex with a little change to \w:\(?:[^\\.]+\)+ , so instead of breaking on a space, it breaks on a dot.
0

Actually, here you may as well do without regex at all. Split the text by ' and use the second part.


As for regex, I would use something more complicated, but allowing to catch other filenames, not just those ending with a 3-letter extension:

'([a-z]:(?:[\\/][^\\/]*)+?)' --

(and use first subpattern from the match)

Comments

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.