高分求一个正则表达式
有一个字符串 <group>hello<group/> world ((baby)) 使其变成 hello world baby ,过滤掉<xxx/>和(())但还保留里边的文字需要注意的是:尖括号里的内容不仅限于group也许是别如比如<people>xxxx<people/> 或者是<people/>的形式。请问如何来写这个正则表达式
--------------------编程问答--------------------
private static void TestRegex07()--------------------编程问答--------------------
{
string yourStr = "<group>hello<group/> world ((baby)) ";
string result = Regex.Replace(yourStr, @"<[^>]+>|[()]", string.Empty);
Console.WriteLine(result);
}
string result = Regex.Replace("<group>hello<group/> world ((baby))",@"(<[^>]*>|\(|\))*",String.Empty);--------------------编程问答-------------------- 呼不会。 --------------------编程问答--------------------
result 的值就为 “hello world baby”
using System;--------------------编程问答-------------------- <[^>]+>|[()]换成<[*]+>|[()]也可以的。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml.Linq;
namespace ConsoleCSharp
{
class Program
{
static void Main(string[] args)
{
string yourStr = "<group>hello<group/> world ((baby)) <people>xxxx<people/>";
string result = Regex.Replace(yourStr, @"<[^>]*>|\(|\)", string.Empty);
Console.WriteLine(result);
}
}
}
初学正则,想问一下,<[^>]+> 这个是用来匹配<>之间的,为什么要用^>,而不用*?[^>]这个是表示任意的不是>的符号吧? --------------------编程问答-------------------- <后面必然要跟个>,你可以用.*?来找。但是非贪婪匹配会每次都要回溯,效率不高。限定一个规则,使用贪婪模式,速度会快,避免回溯。你那样修改了肯定不行。 --------------------编程问答-------------------- 我是楼主我还想问一下,如果我只想去掉两个括号的括号((或))而如果字符串中出现如(abc)只有一个括号则不去掉括号怎么写?谢谢。 --------------------编程问答-------------------- <group>hello<group/> world ((baby)) 使其变成 hello world baby
<[^>]*>|\(\(|\)\) --------------------编程问答-------------------- <[^>]*>|[()]{2,} 我会了,这样也成吧。 --------------------编程问答--------------------
private static void TestRegex07()--------------------编程问答-------------------- 成
{
string yourStr = "<group>hello<group/> world ((baby)) ";
string result = Regex.Replace(yourStr, @"<[^>]+>|([()])\1", string.Empty);
Console.WriteLine(result);
}
回复内容太短了!
--------------------编程问答-------------------- <[^>]*>|[()]{2,}
[\(\)] ()需要加上\转义
{2,}是重复2或2次以上
{2}是重复2次 --------------------编程问答--------------------
学习了,我是初学者,还得再看看。 --------------------编程问答-------------------- 看看········
补充:.NET技术 , C#