简体   繁体   English

正则表达式替换除方括号外的字符串

[英]Regular expression to replace string except in sqaure brackets

Need to replace all forward-slash ( / ) with > except for the ones in the square brackets需要用>替换所有正斜杠( / ),方括号中的除外

input string:输入字符串:

string str = "//div[1]/li/a[@href='https://www.facebook.com/']";

Tried pattern (did not work):尝试过的模式(不起作用):

string regex = @"\/(?=$|[^]]+\||\[[^]]+\]\/)";
var pattern = Regex.Replace(str, regex, ">");

Expected Result:预期结果:

">>div[1]>li>a[@href='https://www.facebook.com/']"

If you're willing to also use String.Replace you can do the following:如果您愿意也使用String.Replace您可以执行以下操作:

string input = "//div[1]/li/a[@href='https://www.facebook.com/']";
        string expected = ">>div[1]>li>a[@href='https://www.facebook.com/']";

        var groups = Regex.Match(input, @"^(.*)(\[.*\])$")
             .Groups
             .Cast<Group>()
             .Select(g => g.Value)
             .Skip(1);
        var left = groups.First().Replace('/', '>');
        var right = groups.Last();
        var actual = left + right;

        Assert.Equal(expected, actual);

What this does is split the string into two groups , where for the first group the / is replaced by > as you describe.这样做是将string分成groups ,其中第一组/被替换为>如您所描述的。 The second group is appended as is.第二group按原样附加。 Basically, you don't care what is between square brackets.基本上,您不关心方括号之间的内容。

(The Assert is from an xUnit unit test.) Assert来自xUnit单元测试。)

You could either match from an opening till a closing square bracket or capture the / in a capturing group.您可以从开始匹配到结束方括号,也可以在捕获组中捕获/

In the replacement replace the / with a <在替换中将/替换为<

Pattern图案

\[[^]]+\]|(/)
  • \[[^]]+\] Match from opening [ till closing ] \[[^]]+\]匹配从开始[直到结束]
  • | Or或者
  • (/) Capture / in group 1 (/)在第 1 组中捕获/

Regex demo |正则表达式演示| C# demo C# 演示

For example例如

string str = "//div[1]/li/a[@href='https://www.facebook.com/']";
string regex = @"\[[^]]+\]|(/)";
str = Regex.Replace(str, regex, m => m.Groups[1].Success ? ">" : m.Value);
Console.WriteLine(str);

Output Output

>>div[1]>li>a[@href='https://www.facebook.com/']

Your thinking was good with lookbehind but instead positive use negative.你的想法是好的,但相反的是积极的使用消极的。

(?<!\[[^\]]*)(\/)

Demo演示

After updating your c# code更新您的 c# 代码后

string pattern = @"(?<!\[[^\]]*)(\/)";
string input = "//div[1]/li/a[@href='https://www.facebook.com/']";
var result = Regex.Replace(input, pattern, ">");

You will get你会得到

>>div[1]>li>a[@href='https://www.facebook.com/']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM