日期:2014-05-18  浏览次数:20685 次

c#字符串提取的问题。
有这么几个字符串:
string str1=“《红与黑》第90章第37页第30行"; //这个字符串要提取出来:90,37,30也就是说提取数字
string str2="《红与黑》第90章第70行"; //这个也是提取90,70数字,

我改怎样吧这些数字从字符串中提取出来呢?
并且我知道我提取的数字对应的是章,还是页,还是行????

------解决方案--------------------
正则表达式,写了段代码:
string str = "《红与黑》第90章第37页第30行";
List< KeyValuePair<int, string>> list = new List<KeyValuePair<int,string>>();
foreach (Match m in Regex.Matches(str, "([\\d]+)([行|页|章])"))
{
list.Add(new KeyValuePair<int, string>(int.Parse(m.Groups[1].Value), m.Groups[2].Value));
}

//Console.WriteLine("With 行|页|章");
foreach (KeyValuePair<int, string> k in list)
Console.Write("{0}({1}), ", k.Key, k.Value);

Console.WriteLine();
//Console.WriteLine("Only number");
foreach (KeyValuePair<int, string> k in list)
Console.Write("{0}, ", k.Key);

输出结果为
90(章), 37(页), 30(行),
90, 37, 30,
------解决方案--------------------
C# code

try {
    Regex regexObj = new Regex(@"\d+");
    Match matchResults = regexObj.Match(subjectString);
    while (matchResults.Success) {
        // matched text: matchResults.Value
        // match start: matchResults.Index
        // match length: matchResults.Length
        matchResults = matchResults.NextMatch();
    } 
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}