using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(?P<content>(?:[-/][a-zA-Z0-9\-_]+)?=?)(?P<prefix>bazel-out|external)/";
string substitution = @"\g<content>dynamically-matched-new-prefix/";
string input = @"-Ibazel-out/k8-fastbuild/include"" → Matches bazel-out/
-Lexternal/foo/lib"" → Matches external/
--gcc-toolchain=bazel-out/toolchain"" → Matches bazel-out/
-fmodule-output=external/module.pcm"" → Matches external/
/Ibazel-out/k8-fastbuild/include"" (MSVC, Windows) → Matches bazel-out\
--extract-api-ignores=external/bar,baz"" → Matches external/
bazel-out/k8-fastbuild/source.c"" (file field) → Matches bazel-out/
Should Not Match
-Ifoo/bazel-out/bar"" (nested path) → No match
-Dmacro=bazel-out_value"" (not a path) → No match
bazel-output/include"" (similar name) → No match
-I externals/foo"" (similar name) → No match
-I bazel-out/..."" (space after -I) → No match (separate flags)";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx