0

やあみんなこれは単純なはずです、私はそれを見ていません、私はそれが主要な現金($)を見つけた後に動作する正規表現(PERL、Awk、SED / * nixの下で実行可能)を作成したいと思います等しい(=)で、二重引用符または一重引用符の最初のインスタンスと二重引用符または一重引用符の最後のインスタンスの間にあるものを処理します。

いくつか例を挙げましょう。

$this = 'operate on some text in here'; # operates between single quotes
$this = "operate on some text in here"; # operates between double quotes
$this = 'operate "on some text" in here'; # operates between single quotes
$this = 'operate \'on some text\' in here'; # operates between outer single quotes

私はいくつかの本当に悪い正規表現を試しました。しかし、それを正しく一致させることができませんでした。

興味のある人のために、これが私が挿入しているものです

printf '$request1 = "select * from whatever where this = that and active = 1 order by something asc";\n' |
grep '{regex}' * |
perl -pe 's/select/SELECT/g ; s/from/\n   FROM/g ; s/where/\n      WHERE/g ; s/and/\n      AND/g ; s/order by/\n         ORDER BY/g ; s/asc/ASC/g ; s/desc/DESC/g ;' | ## enter through file with all clauses
awk '{gsub(/\r/,"");printf "%s\n%d",$0,length($0)}' ## take first line convert to whitespace, use on following lines

みんなありがとう!

4

3 に答える 3

5

一般に、実際のperlコードを解析する場合は、PPIを提案(および使用)します。それ以外の場合は、Regexp::Commonを使用してください

use Regexp::Common;

my @lines = split /\s*\n\s*/, <<'TEST';
$this = 'operate on some text in here'; // operates between single quotes
$this = "operate on some text in here"; // operates between double quotes
$this = 'operate "on some text" in here'; // operates between single quotes
$this = 'operate \'on some text\' in here'; // operates between outer single quotes
TEST

for (@lines)
{
    /$RE{quoted}{-keep}/ && print $1, "\n";
}

与える:

$ perl x.pl
'operate on some text in here'
"operate on some text in here"
'operate "on some text" in here'
'operate \'on some text\' in here'
于 2012-06-21T19:38:37.670 に答える
2

脚本:

@list = <main::DATA>;

foreach (@list) {
  my @x = /^\s*\$(\S+)\s*=\s*(['"])((?:.(?!\2)|\\\2)*.?)\2\s*;/;
  $x[2] =~ s/\\$x[1]/$x[1]/g; # remove backslash before quote character
  print "$x[0]\t$x[2]\n";
}

__DATA__
$this = 'operate on some text in here';     // operates between single quotes
$this = "operate on some text in here";     // operates between double quotes
$this = 'operate "on some text" in here';   // operates between single quotes
$this = 'operate \'on some text\' in here'; // operates between outer single quotes

あなたに与えるでしょう:

this   operate on some text in here
this   operate on some text in here
this   operate "on some text" in here
this   operate 'on some text' in here
于 2012-06-21T21:08:37.717 に答える
0

これはあなたのために働くかもしれません:

 sed 's/\(\$[^=]*=[^'\''"]*\)\(['\''"]\)[^\\'\''"]*\(\\['\''"][^'\''"]*\)*\2/\1\2Replacement\2/' file
于 2012-06-21T21:13:58.767 に答える