string pattern = @”Record\((\s*?)id=(.*?)name=(.*?)time=(.*?)\)”;
string input = textBoxRecords.Text;
Regex regex = new Regex(pattern);
if (regex.IsMatch(input))
{
MatchCollection colls = Regex.Matches(input, pattern);
foreach (Match item in colls)
{
Console.WriteLine(item.Value.ToString());
//去掉record=();
string data;
data = item.Value.ToString();
string a;
a = “(“;
string b;
b = “)”;
int IndexofA = data.IndexOf(a);
int IndexofB = data.IndexOf(b);
string Ru = data.Substring(IndexofA + 1, IndexofB – IndexofA – 1);//得到id=”1″ name=”安” time=”2016-10-18 12:14:16″
Console.WriteLine(“Ru = ” + Ru); //得到id=”1″ name=”安” time=”2016-10-18 12:14:16″
怎么样把id=”1″ name=”安” time=”2016-10-18 12:14:16″值截取出来然后再赋值给id,name,time
string input = textBoxRecords.Text;
Regex regex = new Regex(pattern);
if (regex.IsMatch(input))
{
MatchCollection colls = Regex.Matches(input, pattern);
foreach (Match item in colls)
{
Console.WriteLine(item.Value.ToString());
//去掉record=();
string data;
data = item.Value.ToString();
string a;
a = “(“;
string b;
b = “)”;
int IndexofA = data.IndexOf(a);
int IndexofB = data.IndexOf(b);
string Ru = data.Substring(IndexofA + 1, IndexofB – IndexofA – 1);//得到id=”1″ name=”安” time=”2016-10-18 12:14:16″
Console.WriteLine(“Ru = ” + Ru); //得到id=”1″ name=”安” time=”2016-10-18 12:14:16″
怎么样把id=”1″ name=”安” time=”2016-10-18 12:14:16″值截取出来然后再赋值给id,name,time
解决方案
8
进行拆分 然后split(“=”)[1]赋值
var test = "id="1" name="安" time="2016-10-18 12:14:16""; foreach (var item in test.Split(" ")) { var hh = item.Split("=")[1];//剩下自行处理 }
1
空格分隔,然后等号分隔
10
可以使用多种方法:
下列为方法之一
下列为方法之一
string ru = "id=1 name=安 time=2016-10-18 12:14:16"; string[] arr = ru.Split(" "); string id = arr[0].Substring(arr[0].IndexOf("=")+1); string name = arr[1].Substring(arr[1].IndexOf("=")+1); string time = arr[2].Substring(arr[2].IndexOf("=") + 1) +" "+ arr[3].Substring(arr[3].IndexOf("=") + 1);
1
json.net 之类的序列化很方便的
4
arr[3]没“=”
8
额,哈哈,是
改成
string time = arr[2].Substring(arr[2].IndexOf("=") + 1) +" "+ arr[3]
8
static void Main(string[] args) { var s = "id="1" name="安" time="2016-10-18 12:14:16""; //写在程序里,引号要转义,这你知道的 var m = Regex.Matches(s, @"(\w+)=""([^""]+)"""); foreach (Match x in m) { Console.WriteLine("{0} => {1}", x.Groups[1], x.Groups[2]); } Console.ReadKey(); }