java正则表达式去除尖括号,正则表达式来替换方括号用尖括号

I have a string like:

[a b="c" d="e"]Some multi line text[/a]

Now the part d="e" is optional. I want to convert such type of string into:

Some multi line text

The values of a b and d are constant, so I don't need to catch them. I just need the values of c, e and the text between the tags and create an equivalent xml based expression. So how to do that, because there is some optional part also.

解决方案

For HTML tags, please use HTML parser.

For [a][/a], you can do like following

Match m=Regex.Match(@"[a b=""c"" d=""e""]Some multi line text[/a]",

@"\[a b=""([^""]+)"" d=""([^""]+)""\](.*?)\[/a\]",

RegexOptions.Multiline);

m.Groups[1].Value

"c"

m.Groups[2].Value

"e"

m.Groups[3].Value

"Some multi line text"

Here is Regex.Replace (I am not that prefer though)

string inputStr = @"[a b=""[[[[c]]]]"" d=""e[]""]Some multi line text[/a]";

string resultStr=Regex.Replace(inputStr,

@"\[a( b=""[^""]+"")( d=""[^""]+"")?\](.*?)\[/a\]",

@"$3",

RegexOptions.Multiline);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值