0

I want to create a small tool to convert some code.

Most of it can be done by Regex, so I do not need an actual parser.

But there is a problem with repeating patterns. Below a simplified sample:

var nl = Environment.NewLine;
var temp = 
@"Other things
End
End
Other things";

temp = Regex.Replace(temp, @"(\r\nEnd\r\n)+",@"" + nl + "}" + nl + "");

This will return

Other things
}
End
Other things

So the second End is not replaced. (except if I execute the same Regex.Replace twice)

Any idea how to solve this?

Best regards

2
  • At dotnetperls.com/regex show how to use the regular expression. It is very fast to read and quite interesting !! Commented Feb 22, 2017 at 12:31
  • also, for quick testing:regex101.com Commented Feb 22, 2017 at 12:41

1 Answer 1

4

It happens because matches can't overlap. You have

Other things\r\n
End\r\n
End\r\n
Other things\r\n

that is matched as

Other things***From Here***\r\n
End\r\n***To Here***
End\r\n
Other things

The regex can't match the second \r\nEnd\r\n because the prefixed \r\n has already been captured.

Try:

temp = Regex.Replace(temp, @"(?<=\r\n)End(?=\r\n)+", "}");

Using the non-capturing grouping (?<=) and (?=) you can solve the problem (but note that being non-capturing, you can't replace what is matched in the (?<=) and (?=), so the difference in the replaced text "}")

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.