字符串为: http://g.hello.com/12345  或  http://g.hello.com/12345/
想把数字12345替换成 hello,代码怎么写?可否用正则?

解决方案 »

  1.   

    都不说下替换规则的?"http://g.hello.com/12345".Replace("12345","hello")
      

  2.   

    还是没说清楚规则,取没有子文件夹的完整网址http://g.hello.com/ 然后附加 hello 行不行?
      

  3.   


    Regex reg = new Regex("\d+");
    string str = "http://g.hello.com/12345/";
    string strResult = reg.Replace(str,"hello");
      

  4.   

    你难道这个串中只要有数字就换成hello?
    说清楚什么时候要替换,讲清楚规则,要不然会有很多hello。
    或者仅仅是在http://g.hello.com/的后面,如果是那样那么就直接替换那个区域就OK了
      

  5.   


    string result = Regex.Replace("http://g.hello.com/12345", @"(?<=(?:http://)?[^/]+/)\d+", "hello");楼主在蔑视我们……
      

  6.   

    url还可能为http://g.hello.com/12345/topic  或者 http://g.hello.com/12345/topic/
    还是要替换掉里面的数字,如何? 
      

  7.   

    url = Regex.Replace(url, "[0-9]+", "hello");
      

  8.   

     汗~ 那么 我想问 LZ  你URL  数字区域只有那个地方么? 还是 说其他地方也可以有数字?
    比如
    http://g.hello.com/12345/123如果你只有 /12345/ 这里面是数全部字的话 那么
    就先 以 "/" 截取字符串 
    然后判断那个字符串 是全部数字的 然后用Replace("全部数字的字符串","hello")
      

  9.   

    其他地方可能也有数字哦  如你说的  http://g.hello.com/12345/123 是有可能的 
      

  10.   


    你测试过给你的正则了么。显然你说的这些情况都在我的意料之中,都可以替换的string result = Regex.Replace(@"url还可能为http://g.hello.com/12345/topic 或者 http://g.hello.com/12345/topic/
    还是要替换掉里面的数字,如何?", @"(?<=(?:http://)?[^/]+/)\d+", "hello");