Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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#_Regex - Fatal编程技术网

C# 使用正则表达式将字符串拆分为三个单独的变量

C# 使用正则表达式将字符串拆分为三个单独的变量,c#,regex,C#,Regex,如果我有此格式的字符串: string placeholder = "[[Ford:Focus(blue)]]"; 我可以使用什么正则表达式来填充下面的三个变量 string make = ? string model = ? string colour = ? 有什么想法吗 谢谢,太好了……如果颜色是可选的,但没有指定怎么办……正则表达式中有没有办法将其设置为空字符串?我想您可以使用\w*而不是\w+。@对于第一个正则表达式,您可以检查零件的长度,对于第二个正则表达式,你应该给一个样品输入

如果我有此格式的字符串:

string placeholder = "[[Ford:Focus(blue)]]";
我可以使用什么正则表达式来填充下面的三个变量

string make = ?
string model = ?
string colour = ?
有什么想法吗


谢谢,

太好了……如果颜色是可选的,但没有指定怎么办……正则表达式中有没有办法将其设置为空字符串?我想您可以使用\w*而不是\w+。@对于第一个正则表达式,您可以检查零件的长度,对于第二个正则表达式,你应该给一个样品输入,如果颜色部分有一个空间,它不会被拾取。例如,浅蓝色。有什么建议吗?@炮手然后用[a-zA-Z]替换\w++
string input = "[[Ford:Focus(blue)]]";
var parts = Regex.Matches(input, @"\w+").Cast<Match>().Select(x => x.Value).ToList();

var make = parts[0];
....
var match = Regex.Match(input, @"\[\[(\w+):(\w+)\((\w+)\)\]\]");
var make = match.Groups[1].Value;
var model = match.Groups[2].Value;
....