字符长度是固定,那这段要怎么改呢,对正则不是很熟悉,这代码也参考别人写出来的 Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}"); 我对正则也不熟,你直接截取字符串吧用subString();
var str = "B_F_SENSUS_2G_5322_56.81.13.51.01.28"; Regex r = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}", RegexOptions.IgnoreCase); Match m = r.Match(str); while (m.Success) { var test = m.Groups[0].Value.Replace("_", "").Replace(".", ""); //输出5322568113510128 MessageBox.Show(test); } 已测试。
你这正则跟我这代码差不多啊,你是怎么把2D这2字符给去除的呢? 能说下嘛?小弟对正则不是很懂 这是我代码 if (fileName.Contains("BTG")) { for (int i = 0; i < listBox4.Items.Count; i++) {
Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}"); MatchCollection mc = reg.Matches(this.listBox4.Items[i].ToString()); foreach (Match m in mc) { this.listBox2.Items.Add(Regex.Replace(this.listBox4.Items[i].ToString(), @"\D", string.Empty)); } } }
if (fileName.Contains("BTG")) { for (int i = 0; i < listBox4.Items.Count; i++) {
Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}"); MatchCollection mc = reg.Matches(this.listBox4.Items[i].ToString()); foreach (Match m in mc) { this.listBox2.Items.Add(Regex.Replace(this.listBox4.Items[i].ToString(), @"\D", string.Empty)); } } }我这段代码是什么问题呢? 取出来后怎么是2533..... 多了个2呢?
if (fileName.Contains("BTG")) { for (int i = 0; i < listBox4.Items.Count; i++) { Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}", RegexOptions.IgnoreCase); Match mc = r.Match(this.listBox4.Items[i].ToString()); foreach (Match m in mc) { this.listBox2.Items.Add(m.Groups[0].Value.Replace("_", "").Replace(".", ""))); } } }
Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}"); 我对正则也不熟,你直接截取字符串吧用subString();
Regex r = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}", RegexOptions.IgnoreCase);
Match m = r.Match(str);
while (m.Success)
{
var test = m.Groups[0].Value.Replace("_", "").Replace(".", "");
//输出5322568113510128
MessageBox.Show(test);
}
已测试。
能说下嘛?小弟对正则不是很懂
这是我代码 if (fileName.Contains("BTG"))
{
for (int i = 0; i < listBox4.Items.Count; i++)
{
Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}");
MatchCollection mc = reg.Matches(this.listBox4.Items[i].ToString());
foreach (Match m in mc)
{
this.listBox2.Items.Add(Regex.Replace(this.listBox4.Items[i].ToString(), @"\D", string.Empty)); }
}
}
if (fileName.Contains("BTG"))
{
for (int i = 0; i < listBox4.Items.Count; i++)
{
Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}");
MatchCollection mc = reg.Matches(this.listBox4.Items[i].ToString());
foreach (Match m in mc)
{
this.listBox2.Items.Add(Regex.Replace(this.listBox4.Items[i].ToString(), @"\D", string.Empty)); }
}
}我这段代码是什么问题呢? 取出来后怎么是2533..... 多了个2呢?
{
for (int i = 0; i < listBox4.Items.Count; i++)
{
Regex reg = new Regex(@"(?is)[\d]{4}_[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}.[\d]{2}", RegexOptions.IgnoreCase);
Match mc = r.Match(this.listBox4.Items[i].ToString());
foreach (Match m in mc)
{
this.listBox2.Items.Add(m.Groups[0].Value.Replace("_", "").Replace(".", ""))); }
}
}