字符串:0||201106201025.jpg||66.07 KB||~/Upload/20110714113054468.jpg||file-0#0||未标题-1.jpg||19.34 KB||~/Upload/20110714113059406.jpg||file-1
想取:~/Upload/20110714113054468.jpg和~/Upload/20110714113059406.jpg  public   string GetInString(string content, string beginStr, string endStr)
        {
            Match m = null;
            Regex r = new Regex(beginStr + "(.*?)" + endStr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            string tmpList = "";
            for (m = r.Match(content); m.Success; m = m.NextMatch())
            {
                tmpList = (m.Groups[0].Value);
            }
            return tmpList;
        }
 GetInString(字符串, "~/Upload/", "||");取不出来 请问原因?

解决方案 »

  1.   


    void Main()
    {
    string str="0||201106201025.jpg||66.07 KB||~/Upload/20110714113054468.jpg||file-0#0||未标题-1.jpg||19.34 KB||~/Upload/20110714113059406.jpg||file-1";
    Console.WriteLine( GetInString(str, "~/Upload/", "||"));
    }
    public   string GetInString(string content, string beginStr, string endStr)
    {
    Match m = null;
    Regex r = new Regex(Regex.Escape(beginStr) + "(.*?)" + Regex.Escape(endStr), RegexOptions.IgnoreCase | RegexOptions.Compiled);
    string tmpList = "";
    for (m = r.Match(content); m.Success; m = m.NextMatch())
    {
    tmpList += (m.Value)+"   ";
    }
    return tmpList;
    }
      

  2.   

    结果:  ~/Upload/20110714113054468.jpg||   ~/Upload/20110714113059406.jpg||   
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "0||201106201025.jpg||66.07 KB||~/Upload/20110714113054468.jpg||file-0#0||未标题-1.jpg||19.34 KB||~/Upload/20110714113059406.jpg||file-1";
                foreach (Match m in Regex.Matches(str, @"(?i)~/upload/.*?\.jpg"))
                {
                    Console.WriteLine(m.Value);
                }
                /*
                 * ~/Upload/20110714113054468.jpg
                 * ~/Upload/20110714113059406.jpg
                 */
            }
        }
    }
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    namespace sxLdfang
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = @"0||201106201025.jpg||66.07 KB||~/Upload/20110714113054468.jpg||file-0#0||未标题-1.jpg||19.34 KB||~/Upload/20110714113059406.jpg||file-1";
                string pattern = @"(?i)(?<=kb\|\|)[^|]+";
                MatchCollection mc = Regex.Matches(html, pattern);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }
                Console.ReadKey();
            }
        }
    }
    运行结果:
    ~/Upload/20110714113054468.jpg
    ~/Upload/20110714113059406.jpg
      

  5.   


    //当然这样也是可以的:
    void Main()
    {
    string str="0||201106201025.jpg||66.07 KB||~/Upload/20110714113054468.jpg||file-0#0||未标题-1.jpg||19.34 KB||~/Upload/20110714113059406.jpg||file-1";
    Console.WriteLine( GetInString(str, "~/Upload/", "||"));
    //~/Upload/20110714113054468.jpg   ~/Upload/20110714113059406.jpg 
    }
    public   string GetInString(string content, string beginStr, string endStr)
    {
    Match m = null;
    Regex r = new Regex("("+Regex.Escape(beginStr) + ".*?)" + Regex.Escape(endStr), RegexOptions.IgnoreCase | RegexOptions.Compiled);
    string tmpList = "";
    for (m = r.Match(content); m.Success; m = m.NextMatch())
    {
    tmpList += (m.Groups[1].Value)+"   ";
    }
    return tmpList;
    }