Skip to content

Instantly share code, notes, and snippets.

@hell0again
Created May 19, 2015 09:23
Show Gist options
  • Save hell0again/8730ca8f230a49881aec to your computer and use it in GitHub Desktop.
Save hell0again/8730ca8f230a49881aec to your computer and use it in GitHub Desktop.
perl5.10以降で可能な入れ子を扱う正規表現
my $str = "a + (b * (3 / (c + 5)))";
$str =~ /\(((?:[^()]++|(?R))*)\)/;
# /\( \)/; # 括弧にマッチ
# ( ) # 後方参照
# (?:[^()]++|(?R))* # 括弧以外のバックトラックなしの貪欲マッチ または全体のパターンにマッチ(再帰)
print $1; ## "b * (3 / (c + 5))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment