How to perform a RegEx replace only if another a separate filter is matched using .NET?

Viewed 32

Given a string (a path) that matches /dir1/, I need to replace all spaces with dashes.

Ex: /dir1/path with spaces should become /dir1/path-with-spaces.

This could easily be done in 2 steps...

var rgx = new Regex(@"^\/dir1\/");
var path = "/dir1/path with spaces";
if (rgx.isMatch(path))
{
    path = (new Regex(@" |\%20")).Replace(path, "-");
}

Unfortunately for me, the application is already built with a simple RegEx replace and cannot be modified, so I need to have the RegEx do the work. I thought I had found the answer here: regex: how to replace all occurrences of a string within another string, if the original string matches some filter

And was able create and test (?:\G(?!^)|^(?=\/dir1\/.*$)).*?\K( |\%20), but then I learned it does not work in this app because the \K is an unrecognized escape sequence (not supported in .NET).

I also tried a positive lookbehind, but I wasn't able to get it to replace all the spaces (only the last if the match was greedy or the first if not greedy). I could put in enough checks to handle the max number of spaces, but as soon as I check for 10 spaces, someone will pass in a path with 11 spaces.

Is there a RegEx only solution for this problem that will work in the .NET engine?

1 Answers

You can leverage the unlimited width lookbehind pattern in .NET:

Regex.Replace(path, @"(?<=^/dir1/.*?)(?: |%20)", "-")

See the regex demo

enter image description here

Regex details

  • (?<=^/dir1/.*?) - a positive lookbehind that matches a location that is immediately preceded with /dir1/ and then any zero or more chars other than a newline char, as few as possible
  • (?: |%20) - either a space or %20 substring.
Related