提前FFFF前的数字 String s = @"00000000000000FFFF00FFFF"; Regex reg = new Regex(@"[\d]+(?=FFFF)"); MatchCollection ms = reg.Matches(s); foreach (Match match in ms) { MessageBox.Show(match.Value); }
match.Vaule怎么把你的代码放入我下面的代码中啊for (int i = 0; i < ds.Tables[0].Rows.Count; i++) {
string a = ds.Tables[0].Rows[i]["path"].ToString().Replace("F", "");; MatchCollection mc = Regex.Matches(b, @"\d{4}"); string value = ""; foreach (Match m in mc) { MatchCollection mcc = Regex.Matches(m.Value, @"\d{2}"); value += mcc[1].Value + mcc[0].Value; } ds.Tables[0].Rows[i]["path"] =value;
你把 string a = ds.Tables[0].Rows[i]["path"].ToString() 这句替换掉 string a = "00000000000000FFFF00FFFF";这句不就行了然后把ds.Tables[0].Rows[i]["path"] =value;加在 string value = a.Substring(0, i * 4);//这个就是你要的值 这句后面
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { string a = ds.Tables[0].Rows[i]["path"].ToString(); for (int i = 0; i < a.Length / 4; i++) { if (a.Substring(i * 4, 4) == "FFFF") { string value = a.Substring(0, i * 4);//这个就是你要的值 ds.Tables[0].Rows[i]["path"] = value; break; } } } this.dataGridView1.DataSource = ds.Tables[0];
额,我表达的不清楚,我的意思是 把我的代码合并,下面的代码也要的,四个数两两交换位置的 foreach (Match m in mc) { MatchCollection mcc = Regex.Matches(m.Value, @"\d{2}"); value += mcc[1].Value + mcc[0].Value; }
Regex reg = new Regex(@"[\d]+(?=FFFF)");
MatchCollection ms = reg.Matches(s);
foreach (Match match in ms)
{
MessageBox.Show(match.Value);
}
{
string a = ds.Tables[0].Rows[i]["path"].ToString().Replace("F", "");;
MatchCollection mc = Regex.Matches(b, @"\d{4}");
string value = "";
foreach (Match m in mc)
{
MatchCollection mcc = Regex.Matches(m.Value, @"\d{2}");
value += mcc[1].Value + mcc[0].Value;
}
ds.Tables[0].Rows[i]["path"] =value;
}
this.dataGridView1.DataSource = ds.Tables[0];
如果你的数据是如下形式:
000F,FFFF
那么按照他们的方法,你会得到000,而不是000F
所以先分组,再判断,而不是遇到FFFF就认为前面的都刚好能被4整除了还有可能是如下的数据:
000F,FFF0,FFFF
正常应该匹配000F,FFF0
而遇到FFFF就截取,会只取到000
string a = ds.Tables[0].Rows[i]["path"].ToString()
这句替换掉
string a = "00000000000000FFFF00FFFF";这句不就行了然后把ds.Tables[0].Rows[i]["path"] =value;加在
string value = a.Substring(0, i * 4);//这个就是你要的值
这句后面
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string a = ds.Tables[0].Rows[i]["path"].ToString();
for (int i = 0; i < a.Length / 4; i++)
{
if (a.Substring(i * 4, 4) == "FFFF")
{
string value = a.Substring(0, i * 4);//这个就是你要的值
ds.Tables[0].Rows[i]["path"] = value;
break;
}
} }
this.dataGridView1.DataSource = ds.Tables[0];
{
MatchCollection mcc = Regex.Matches(m.Value, @"\d{2}");
value += mcc[1].Value + mcc[0].Value;
}
static void Main(string[] args)
{
//定义一个字符串,用来存储文件全路径
string strAllPath = "D:\\粤系菜谱(珍藏版)\\皇宫菜系(标准版).exe";
string strPath = strAllPath.Substring(0, strAllPath.LastIndexOf("\\") + 1);//获取文件路径
string strName = strAllPath.Substring(strAllPath.LastIndexOf("\\") + 1);//获取文件名
Console.WriteLine("文件路径:" + strPath);//显示文件路径
Console.WriteLine("文件名:" + strName);//显示文件名
Console.ReadLine();
}