Skip to content

Instantly share code, notes, and snippets.

@tateisu
Last active November 15, 2018 08:26
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 tateisu/c88936befcad987cf25e2883ec5d7e72 to your computer and use it in GitHub Desktop.
Save tateisu/c88936befcad987cf25e2883ec5d7e72 to your computer and use it in GitHub Desktop.
JSON から // コメント を除去するPerl
#!perl --
use strict;
use warnings;
my $a = <<"END";
{
"key":"va//lue", // comment
/ // comment
}
END
$a =~ s`
^ # 行頭。正規表現のmフラグにより、各行の先頭にマッチする
( # 行頭のコメントじゃない部分は
(?: [^/"]+ # ・" でも / でもない文字
| "(?:[^"]+|\\")*" # ・"" で囲われた文字列
| /(?!/) # ・連続しない /
)*? # 上記いずれかのパターンの繰り返しである
)
// # コメント開始部分
[^\x0d\x0a*]* # 改行文字以外の文字列
(?=[\x0d\x0a\\z]) # 肯定先読み:改行文字または終端
`$1`gmx;
print $a;
__END__
output:
{
"key":"va//lue",
/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment