使用正则表达式将字符串拆分为三个单独的变量 [英] split string with regex into three separate variables

查看:42
本文介绍了使用正则表达式将字符串拆分为三个单独的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个这种格式的字符串:

If I have a string in this format:

string placeholder = "[[Ford:Focus(blue)]]";

我可以使用什么正则表达式来填充以下三个变量?

What regex could I use to populate the three variables below?

string make = ?
string model = ?
string colour = ?

有什么想法吗?

谢谢,

推荐答案

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;
....

这篇关于使用正则表达式将字符串拆分为三个单独的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆