string tempStr = @"aaabbbcccaaagggaaa123456789"; string pattern = @"(?<=([\s\S]*aaa){3}[\s\S]{7}).";//aaa相当于字符串B 3相当于n 7相当于m+1 MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.Multiline|RegexOptions.IgnoreCase); for (int i = 0; i < mc.Count; i++) { string ss = mc[i].Value;//得到8 //string ss = mc[i].Groups["id"].Value; //string text = mc[i].Groups["text"].Value;// }
string str = "122bbbbb12345"; string regex_str = @"[b]{5}[\d]{5}";//5个b后面的第5个数字 Regex r = new Regex(regex_str); // 定义一个Regex对象实例 Match m = r.Match(str); // 在字符串中匹配 Console.WriteLine(str[m.Index + 9]);//m.Index表示匹配这个字串的位置,str[m.Index + 9]就是这第五个数字
public string getFifthDigit(string s) { string regex_str = @"[b]{5}[\d]{5}";//5个b后面的第5个数字 Regex r = new Regex(regex_str); // 定义一个Regex对象实例 Match m = r.Match(s); // 在字符串中匹配 return str[m.Index + 9];//m.Index表示匹配这个字串的位置,str[m.Index + 9]就是这第五个数字}
public string getFifthDigit(string str) { string regex_str = @"[b]{5}[\d]{5}";//5个b后面的第5个数字 Regex r = new Regex(regex_str); // 定义一个Regex对象实例 Match m = r.Match(str); // 在字符串中匹配 return str[m.Index + 9];//m.Index表示匹配这个字串的位置,str[m.Index + 9]就是这第五个数字 }
啊,我这个问的有点儿问题,如果是找第n个B之后的 第 m 个 数 呢??比如:有个字符串 A = @“ a aa aa aa aa aa aa 0.1 0.2 -0.3 4 5 6.0 -7.0";有个字符串 B = ”aa ”:现在要找 第 6 个 “aa ” 之后的 第 7 个数 ,是“-7.0”不知道这个例子说明了我的意图没
string tempStr = @"a aa aa aa aa aa aa 0.1 0.2 -0.3 4 5 6.0 -7.0"; string pattern = @"(?<=([\s\S]*aa){6}([\s\S]*\d*(\.\d*)?[\s]+){7})\S+";//aa就是B 6 7分别对应 你懂的 MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.Multiline|RegexOptions.IgnoreCase); for (int i = 0; i < mc.Count; i++) { string ss = mc[i].Value;//得到-7.0 //string ss = mc[i].Groups["id"].Value; //string text = mc[i].Groups["text"].Value;//
}
string A = " a aa aa aa aa aa aa 0.1 0.2 -0.3 4 5 6.0 -7.0"; string B = "aa"; int index = 7; Regex reg = new Regex(string.Format(@"{0}(\s[\d.-]+)+", B)); Console.WriteLine(reg.Match(A).Groups[1].Captures[index - 1].Value); //-7.0
如果A=“a bb 1 bb 1 2 bb 1 -7.0 3 cc 1 cc 1 2 cc 1 2 3 ";要找第 3 个 bb 之后的第 2 个数 “-7.0”呢???
两个例子,index是要找的第几个数,例如第2个数为-7.0,第7个数-7.0 strIndex为第几个bb或aa string A = "a bb 1 bb 1 2 bb 1 -7.0 3 cc 1 cc 1 2 cc 1 2 3 "; string B = "bb"; int index = 2; int strIndex = 3; Regex reg = new Regex(string.Format(@"(?:\s{0}(?:\s[\d.-]*)*){{{1}}}\s{0}(\s[\d.-]+)+", B, strIndex - 1)); Console.WriteLine(reg.Match(A).Groups[1].Captures[index - 1].Value); ================================================================================ A = " a aa aa aa aa aa aa 0.1 0.2 -0.3 4 5 6.0 -7.0"; B = "aa"; index = 7; strIndex = 5; reg = new Regex(string.Format(@"(?:\s{0}(?:\s[\d.-]*)*){{{1}}}\s{0}(\s[\d.-]+)+", B, strIndex - 1)); Console.WriteLine(reg.Match(A).Groups[1].Captures[index - 1].Value);
string tempStr = @"aaabbbcccaaagggaaa123456789"; string pattern = @"(?<=([\s\S]*aaa){3}[\s\S]{7}).";//aaa相当于字符串B 3相当于n 7相当于m+1
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.Multiline|RegexOptions.IgnoreCase);
for (int i = 0; i < mc.Count; i++)
{
string ss = mc[i].Value;//得到8
//string ss = mc[i].Groups["id"].Value;
//string text = mc[i].Groups["text"].Value;// }
string str = "122bbbbb12345";
string regex_str = @"[b]{5}[\d]{5}";//5个b后面的第5个数字
Regex r = new Regex(regex_str); // 定义一个Regex对象实例
Match m = r.Match(str); // 在字符串中匹配
Console.WriteLine(str[m.Index + 9]);//m.Index表示匹配这个字串的位置,str[m.Index + 9]就是这第五个数字
public string getFifthDigit(string s)
{
string regex_str = @"[b]{5}[\d]{5}";//5个b后面的第5个数字
Regex r = new Regex(regex_str); // 定义一个Regex对象实例
Match m = r.Match(s); // 在字符串中匹配
return str[m.Index + 9];//m.Index表示匹配这个字串的位置,str[m.Index + 9]就是这第五个数字}
public string getFifthDigit(string str)
{
string regex_str = @"[b]{5}[\d]{5}";//5个b后面的第5个数字
Regex r = new Regex(regex_str); // 定义一个Regex对象实例
Match m = r.Match(str); // 在字符串中匹配
return str[m.Index + 9];//m.Index表示匹配这个字串的位置,str[m.Index + 9]就是这第五个数字
}
string tempStr = @"a aa aa aa aa aa aa 0.1 0.2 -0.3 4 5 6.0 -7.0"; string pattern = @"(?<=([\s\S]*aa){6}([\s\S]*\d*(\.\d*)?[\s]+){7})\S+";//aa就是B 6 7分别对应 你懂的 MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.Multiline|RegexOptions.IgnoreCase);
for (int i = 0; i < mc.Count; i++)
{
string ss = mc[i].Value;//得到-7.0
//string ss = mc[i].Groups["id"].Value;
//string text = mc[i].Groups["text"].Value;//
}
string A = " a aa aa aa aa aa aa 0.1 0.2 -0.3 4 5 6.0 -7.0";
string B = "aa";
int index = 7;
Regex reg = new Regex(string.Format(@"{0}(\s[\d.-]+)+", B));
Console.WriteLine(reg.Match(A).Groups[1].Captures[index - 1].Value);
//-7.0
如果A=“a bb 1 bb 1 2 bb 1 -7.0 3 cc 1 cc 1 2 cc 1 2 3 ";要找第 3 个 bb 之后的第 2 个数 “-7.0”呢???
strIndex为第几个bb或aa string A = "a bb 1 bb 1 2 bb 1 -7.0 3 cc 1 cc 1 2 cc 1 2 3 ";
string B = "bb";
int index = 2;
int strIndex = 3;
Regex reg = new Regex(string.Format(@"(?:\s{0}(?:\s[\d.-]*)*){{{1}}}\s{0}(\s[\d.-]+)+", B, strIndex - 1));
Console.WriteLine(reg.Match(A).Groups[1].Captures[index - 1].Value);
================================================================================
A = " a aa aa aa aa aa aa 0.1 0.2 -0.3 4 5 6.0 -7.0";
B = "aa";
index = 7;
strIndex = 5;
reg = new Regex(string.Format(@"(?:\s{0}(?:\s[\d.-]*)*){{{1}}}\s{0}(\s[\d.-]+)+", B, strIndex - 1));
Console.WriteLine(reg.Match(A).Groups[1].Captures[index - 1].Value);