简单的正则,请大家帮忙

红皮猴 2009-10-13 12:06:30
如何把字符串末尾的<br>p用正则去除如下面4段字符串:
asdadasd<br>p<br>p<br>p<br>p
bbbbbbbb<br>p<br>p
cccccccc<br>p<br>p
dddddddd<br>p<br>p<br>p<br>p
替换后
asdadasd
bbbbbbbb
cccccccc
dddddddd
...全文
105 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2009-10-13
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Test
{
static void Main()
{
string str = @"asdadasd <br>p <br>p <br>p <br>p ";
Console.WriteLine(Regex.Replace(str,@"<br>p",""));
}
}
}
十八道胡同 2009-10-13
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 huigy 的回复:]
狗哥,谢了
[/Quote]
飞翔的狗的几种答案在我这里怎么都是错的?lz你用的他的那种解决的?
红皮猴 2009-10-13
  • 打赏
  • 举报
回复
狗哥,谢了
goodbetter_4107607 2009-10-13
  • 打赏
  • 举报
回复
Regex.Replace("aaa", @"( ?<br>p ?)*$", "", RegexOptions.IgnoreCase);
回复时他怎么总给我多个加个空格啊,第一个?号后面没有空格的。
红皮猴 2009-10-13
  • 打赏
  • 举报
回复
能不能不用循环,一次就替换完毕。偶的字符串数量太多
十八道胡同 2009-10-13
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Test
{
static void Main()
{
string str = @"ccccc <br>p <br>pccc <br>p <br>p", str1 = str;
while ((str1 = Regex.Replace(str, @"\s*<br>p\s*$", "")).Length != str.Length)
str = str1;
Console.WriteLine(str1);
}
}
}

这个方法很罗嗦,等好的办法。
红皮猴 2009-10-13
  • 打赏
  • 举报
回复
不行只能匹配出一个。
红色是我拷贝的时候弄上去的。
fhjzgsy 2009-10-13
  • 打赏
  • 举报
回复
命名空间using System.Text.RegularExpressions;
protected void Button1_Click(object sender, EventArgs e)
{
string str1 = "asdadasd <br>p <br>p <br>p <br>p";
string pattern = "<br>p";

string[] str2 =Split(str1, pattern);
foreach (string i in str2)
Response.Write(i.ToString());

}
public static string[] Split(string input,string pattern)
{
string[] arr = System.Text.RegularExpressions.Regex.Split(input,pattern);
return arr;
}
SK_Aqi 2009-10-13
  • 打赏
  • 举报
回复
顶了
goodbetter_4107607 2009-10-13
  • 打赏
  • 举报
回复
更正
str = Regex.Replace(str , @"( *<br>p *)*$", "", RegexOptions.IgnoreCase);
goodbetter_4107607 2009-10-13
  • 打赏
  • 举报
回复
string str = @"asdadasd <br>p <br>p <br>p <br>p ";
str = Regex.Replace(str , @"( *<br>p *)*$", "", RegexOptions.IgnoreCase);
楼主试试
十八道胡同 2009-10-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 huigy 的回复:]
如何把字符串末尾的 <br>p用正则去除(其它位置的保留)如下面4段字符串:
a <br>p sdadasd <br>p <br>p <br>p <br>p
b <br>p bbbb <br>p bbb <br>p <br>p
ccccc <br>p <br>pccc <br>p <br>p
dddd <br>p dddd <br>p <br>p <br>p <br>p
替换后
a <br>p sdadasd
b <br>p bbbb
ccccc <br>p <br>pccc <br>p <br>p
dddd <br>p dddd
[/Quote]
红色部分?
红皮猴 2009-10-13
  • 打赏
  • 举报
回复
如何把字符串末尾的 <br>p用正则去除(其它位置的保留)如下面4段字符串:
a<br>p sdadasd <br>p <br>p <br>p <br>p
b<br>p bbbb<br>p bbb <br>p <br>p
ccccc<br>p<br>pccc <br>p <br>p
dddd<br>p dddd <br>p <br>p <br>p <br>p
替换后
a<br>p sdadasd
b<br>p bbbb
ccccc<br>p<br>pccc <br>p <br>p
dddd<br>p dddd

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧