Last active
November 15, 2018 08:26
-
-
Save tateisu/c88936befcad987cf25e2883ec5d7e72 to your computer and use it in GitHub Desktop.
JSON から // コメント を除去するPerl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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