コメント、複数行コメント、空行を削除する正規表現
-
22-07-2019 - |
質問
ファイルを解析し、phpとregexを使用して削除したい:
- 空行または空行
- 単一行のコメント
- 複数行コメント
基本的に、次を含む行を削除したい
/* text */
または複数行のコメント
/***
some
text
*****/
可能であれば、行が空かどうかを確認する別の正規表現(空白行を削除)
それは可能ですか?誰かがそれを行う正規表現を私に投稿できますか?
どうもありがとう。
解決
$text = preg_replace('!/\*.*?\*/!s', '', $text);
$text = preg_replace('/\n\s*\n/', "\n", $text);
他のヒント
解析するファイルにこれらの条件に一致する何かを含む文字列がある場合、使用する正規表現はすべて失敗することに注意してください。たとえば、次のようになります。
print "/* a comment */";
これに:
print "";
おそらくあなたが望むものではありません 。しかし、多分それは、私にはわかりません。とにかく、正規表現は技術的にその問題を回避する方法でデータを解析できません。技術的に言うと、現代のPCRE正規表現は多くのハッキングに取り組み、これを可能にし、さらに重要なことに、もはや正規の表現ではなく、何でもできるようにしました。これらのことを引用符の中や他の状況で取り除くことを避けたい場合、本格的なパーサーに代わるものはありません(それでもかなり単純なことができます)。
// Removes multi-line comments and does not create
// a blank line, also treats white spaces/tabs
$text = preg_replace('!^[ \t]*/\*.*?\*/[ \t]*[\r\n]!s', '', $text);
// Removes single line '//' comments, treats blank characters
$text = preg_replace('![ \t]*//.*[ \t]*[\r\n]!', '', $text);
// Strip blank lines
$text = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $text);
それは可能です 、しかし私はそれをしません。 PHPファイル全体を解析して、必要な空白(文字列、キーワード/識別子間の空白(publicfuntiondoStuff())など)が削除されていないことを確認する必要があります。 PHPの tokenizer拡張機能を使用することをお勧めします。
これは、すべての/ *を* /に置き換えるときに機能します。
$string = preg_replace('/(\s+)\/\*([^\/]*)\*\/(\s+)/s', "\n", $string);
$string = preg_replace('#/\*[^*]*\*+([^/][^*]*\*+)*/#', '', $string);
これは正規表現に使用されていない場合の私の解決策です。次のコードは、#で区切られたすべてのコメントを削除し、このスタイルの変数の値を取得しますNAME = VALUE
$reg = array();
$handle = @fopen("/etc/chilli/config", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$start = strpos($buffer,"#") ;
$end = strpos($buffer,"\n");
// echo $start.",".$end;
// echo $buffer ."<br>";
if ($start !== false)
$res = substr($buffer,0,$start);
else
$res = $buffer;
$a = explode("=",$res);
if (count($a)>0)
{
if (count($a) == 1 && !empty($a[0]) && trim($a[0])!="")
$reg[ $a[0] ] = "";
else
{
if (!empty($a[0]) && trim($a[0])!="")
$reg[ $a[0] ] = $a[1];
}
}
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
これは優れた機能であり、機能します!
<?
if (!defined('T_ML_COMMENT')) {
define('T_ML_COMMENT', T_COMMENT);
} else {
define('T_DOC_COMMENT', T_ML_COMMENT);
}
function strip_comments($source) {
$tokens = token_get_all($source);
$ret = "";
foreach ($tokens as $token) {
if (is_string($token)) {
$ret.= $token;
} else {
list($id, $text) = $token;
switch ($id) {
case T_COMMENT:
case T_ML_COMMENT: // we've defined this
case T_DOC_COMMENT: // and this
break;
default:
$ret.= $text;
break;
}
}
}
return trim(str_replace(array('<?','?>'),array('',''),$ret));
}
?>
この関数「strip_comments」を使用して、いくつかの変数に含まれるコードを渡すようになりました:
<?
$code = "
<?php
/* this is comment */
// this is also a comment
# me too, am also comment
echo "And I am some code...";
?>";
$code = strip_comments($code);
echo htmlspecialchars($code);
?>
結果は
として出力されます<?
echo "And I am some code...";
?>
phpファイルからの読み込み:
<?
$code = file_get_contents("some_code_file.php");
$code = strip_comments($code);
echo htmlspecialchars($code);
?>
phpファイルをロードし、コメントを削除して保存する
<?
$file = "some_code_file.php"
$code = file_get_contents($file);
$code = strip_comments($code);
$f = fopen($file,"w");
fwrite($f,$code);
fclose($f);
?>
これが私に合っていることがわかりました、(\ s +)\ / \ *([^ \ /] *)\ * / \ n *
は、複数行のタブ付きまたはタブなしを削除しますコメントとその後ろのスペース。この正規表現に一致するコメントの例を残します。
/**
* The AdditionalCategory
* Meta informations extracted from the WSDL
* - minOccurs : 0
* - nillable : true
* @var TestStructAdditionalCategorizationExternalIntegrationCUDListDataContract
*/