Skip to content

Instantly share code, notes, and snippets.

@siracusa
Last active October 10, 2019 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siracusa/4e139a38b9eff151b5c8a7f3071a0bd7 to your computer and use it in GitHub Desktop.
Save siracusa/4e139a38b9eff151b5c8a7f3071a0bd7 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
# Increase this number as your sanity allows
use constant PAREN_DEPTH => 5;
my $nested_string_escape_regex = '\\\\\(' . '(?:[^()]|\(' x PAREN_DEPTH . '[^()]*' . '\))*' x PAREN_DEPTH . '\)';
my @test_cases = (
'(one)' => [],
'\(one)' => [ '\(one)' ],
'(one \\(two))' => [ '\\(two)' ],
'(one \\(two (three (four))))' => [ '\\(two (three (four)))' ],
'(one (two \(three (four))))' => [ '\\(three (four))' ],
'(one (two \(three (four)))) five \(six (seven (eight (nine))) ten)' => [ '\\(three (four))', '\\(six (seven (eight (nine))) ten)' ],
);
plan tests => @test_cases / 2;
for (my $i = 0; $i < @test_cases; $i += 2) {
my $string = $test_cases[$i];
my $expect = $test_cases[$i + 1];
my @match = ($string =~ /.*?($nested_string_escape_regex)/g);
is_deeply(\@match, $expect, $string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment