It has become very common and thus having a test without a solution for me as I badly blundered because of these test for a specific candidature make me think that it may has become very interesting to keep solution somewhere on my website.
This is the acceptation tests :
using NUnit.Framework;
namespace maximilien.zakowski.Tests
{
public class Tests
{
private Library BuildTested()
{
return new ParenthesesBracketsPatternTester();
}
[TestCase("()")]
[TestCase("[]")]
[TestCase("[()]")]
[TestCase("[[()]]")]
[TestCase("[()[]]")]
[TestCase("([()[]])")]
public void Test1(string validated)
{
var tested = BuildTested();
Assert.IsTrue(tested.IsValid(validated));
}
}
}
The solution to this problems is the regex Pattern recursion :
Source : https://www.regular-expressions.info/refrecurse.html
The solution underneath :
using System.Text.RegularExpressions;
namespace maximilien.zakowski
{
public class ParenthesesBracketsPatternTester
{
public bool IsValid(string validated)
{
var regex = new Regex("(?'group'(\\(\\)|\\[\\]))((?'group')|(\\((?'group')\\))|(\\[(?'group')\\]))");
return regex.IsMatch(validated);
}
}
}