Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 忽略大括号中的值_C#_.net_Regex_Regex Lookarounds - Fatal编程技术网

C# 忽略大括号中的值

C# 忽略大括号中的值,c#,.net,regex,regex-lookarounds,C#,.net,Regex,Regex Lookarounds,我需要: "{branchId}/GetUser/{userId}" -> "{branch-id}/-get-user/{user-id}" 如何忽略大括号中的值?这只是“并非所有问题都必须用正则表达式解决”的一个例子 RegEx解决方案可能是 public static string ToKebabCase(this string value) { if (string.IsNullOrEmpty(value)) return value; var lis

我需要:

"{branchId}/GetUser/{userId}" -> "{branch-id}/-get-user/{user-id}"

如何忽略大括号中的值?

这只是“并非所有问题都必须用正则表达式解决”的一个例子


RegEx
解决方案可能是

public static string ToKebabCase(this string value)
{
   if (string.IsNullOrEmpty(value))
      return value;

   var list = value.Split('/');
   list[1] = Regex.Replace(list[1], "([A-Z])", "-$1").ToLower();

   return string.Join("/", list).Trim();
}
大写字母

(?<!/)
不在括号内

([A-Z])

也许您可以在组1中捕获
{branchId}
,在组2中捕获
Get
,在组3中捕获
User
,在组4中捕获
{userId}
作为示例字符串,并使用:

在替换中,您将使用
$1$2-$3$4

(?![^\{\}]*\})

所有变量都返回{branchid}/get user/{userid},但我需要{branchid}/get user/{userid}。有可能吗?@AlexanderIvanov你需要第一个,我已经更新了
(?<!/)
([A-Z])
(?![^\{\}]*\})
public static string ToKebabCase(this string value)
{
    if (string.IsNullOrEmpty(value))
        return value;

    Regex r1 = new Regex(@"({[^}]+})(\/[A-Z][a-z]+)([A-Z][a-z]+\/)({[^}]+})");
    Match match = r1.Match(value);
    if (match.Success) {
        value = String.Format("{0}{1}-{2}{3}", 
            match.Groups[1].Value,
            match.Groups[2].Value.ToLower(),
            match.Groups[3].Value.ToLower(),
            match.Groups[4].Value
        );           
    }
    return value;
}