If you look at the output of the code; you'll see that once converted, the next regex converts it back..#!/usr/bin/perl -w use strict; my %subs = ( qr/(\d+)F/i => sub { warn "Matched F with $1\n"; return sprintf("%.u" , ($1 - 32) / 1.8) . 'C' }, qr/(\d+)C/i => sub { warn "Matched C with $1\n"; return sprintf("%.u" , ($1 * 1.8) + 32) . 'F' }, ); my $html = do { local $/; <DATA> }; warn $html; foreach my $key (keys %subs) { warn "checking key: $key\n"; $html =~ s/$key/$subs{$key}->()/eg; warn $html; } warn $html; __DATA__ convert 180F to C convert 180C to F convert 30" to cm etc
I assume the solution is to use the global match modifier \G within a while loop but can't get my head around it - if possible I'd like to maintain a hash of qr's to make the code easier to maintain as I extend it. Hope someone can help point me in the right direction! Thanksconvert 180F to C convert 180C to F convert 30" to cm etc checking key: (?i-xsm:(\d+)C) Matched C with 180 convert 180F to C convert 356F to F convert 30" to cm etc checking key: (?i-xsm:(\d+)F) Matched F with 180 Matched F with 356 convert 82C to C convert 180C to F convert 30" to cm etc convert 82C to C convert 180C to F convert 30" to cm etc
In reply to Global replace issue by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |