ehi*_*ime 0 regex perl awk sed
嘿伙计这应该很简单,我只是没有看到它,我想创建一个正则表达式(可在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
Run Code Online (Sandbox Code Playgroud)
我尝试了一些非常糟糕的正则表达式.但只是无法让它匹配正确.
这是我插入的内容,以防任何人感兴趣
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
Run Code Online (Sandbox Code Playgroud)
多谢你们!
通常,如果您正在解析实际的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";
}
Run Code Online (Sandbox Code Playgroud)
得到:
$ 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'
Run Code Online (Sandbox Code Playgroud)