sub parse_bin { my $save = pos; my ($size_sz) = /\G \# (\d) /xgc or goto BACKTRACK; my ($bin_sz) = /\G (\d{$size_sz}) /xgc or goto BACKTRACK; my ($bin) = /\G .{$bin_sz} /xgcs or goto BACKTRACK; return $bin; BACKTRACK: pos = $save; return (); } my $outputText = ''; for ($inputText) { pos = 0; for (;;) { if (my ($bin) = parse_bin()) { $outputText .= '<Binary block: '.length($bin).' bytes>'; next; } if (/\G (.[^#]+ ) /xgcs) { $outputText .= $1; next; } last; } }
Update: The parent's update was updated to generate $newstr since I started. Note that even with the update, the code doesn't behave as a regexp would. It silently drops bits of text instead of backtracking. For example, the "#" in "What you're #?" is dropped, and so is "#1" in "You're #1!".
In reply to Re^2: Can I use backreferences as quantifiers in a regex?
by ikegami
in thread Can I use backreferences as quantifiers in a regex?
by johnbo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |