Zha*_*Chn 0 regex perl parsing pattern-matching
如果块不相互嵌套或自身嵌套,我如何在以下语句中匹配多行块:
if (EXPR) BLOCK
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
unless (EXPR) BLOCK
unless (EXPR) BLOCK else BLOCK
unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL until (EXPR) BLOCK
LABEL until (EXPR) BLOCK continue BLOCK
LABEL for (EXPR; EXPR; EXPR) BLOCK
LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK
LABEL BLOCK continue BLOCK
Run Code Online (Sandbox Code Playgroud)
如果使用正则表达式无法做到这一点,是否有一个状态机能够这样做?
不要试图自己解析perl.如果你真的需要,使用PPI,这是一个不错的工作.
use PPI;
my $source = 'LABEL: { print "block" } continue { print "cont block" }';
my $doc = PPI::Document->new(\$source);
my $blocks = $doc->find("PPI::Structure::Block");
print ":$_:\n" for @$blocks;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
252 次 |
| 最近记录: |