Sandeep Dhamale
Sandeep Dhamale

Reputation: 489

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/']"

Upvotes: 3

Views: 779

Answers (3)

Pietro
Pietro

Reputation: 443

Your thinking was good with lookbehind but instead positive use negative.

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

Demo

After updating your c# code

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/']

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163342

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

Regex demo | C# demo

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

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

Upvotes: 0

Aage
Aage

Reputation: 6252

If you're willing to also use String.Replace you can do the following:

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. The second group is appended as is. Basically, you don't care what is between square brackets.

(The Assert is from an xUnit unit test.)

Upvotes: 0

Related Questions