STLDev
STLDev

Reputation: 6164

Regex match matches one too many characters

I have a need to perform a somewhat strange regular expression replacement. I've just about got it worked out, but not quite.

I need to remove multiple substrings from a string where the substrings to remove are surrounded by square braces [] except where the square braces are followed by two hashtags []##.

For instance, if the original string is:

[phase]This is []a test [I]## of the emergency broadcast system. [28]##[test]xyz

Then the expected output after the regex replace would be:

This is a test [I]## of the emergency broadcast system. [28]##xyz

So far, I've tried a few things, but the closest regex pattern string I've come up with is "\[[^\]]*\][^##]". The problem with this is that it matches one more character than it should. For instance, using the test string above, and doing the regex replace with an empty string, it returns:

his is  test [I]## of the emergency broadcast system. [28]##yz

What is the regex pattern string for which I'm searching?

Upvotes: 1

Views: 754

Answers (2)

AgentFire
AgentFire

Reputation: 9780

Replace your "\[[^\]]*\][^##]" with "(\[[^\]]*\])(([^#]{2})?)".

First, your [^##] does not follow the rule "except where the square braces are followed by two hashtags []##". So it has to be changed to two non-sharpsigh chars.

Second, try replacing like this:

var s= Regex.Replace(input, pattern, "$2");

Upvotes: 0

Adrian Wragg
Adrian Wragg

Reputation: 7401

Your problem is that your additional part,

[^##]

will match the next character that is not a # character. You need a negative lookahead:

\[[^\]]*\](?!##)

Upvotes: 1

Related Questions