Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a string. I have a regexp. This regexp matches in the string multiple times. I need to manipulate the part of the string which is matched each time it matches. I do not know how many times it will match. Example:
$string = "&#15123 la di da &#32714 do bi do &#04271";
I need to then be able to manipulate each instance of "&#\d\d\d\d\d" such as then outputting characters using the ASCII equivalents
print chr(matched numbers);
Can anyone help?

Replies are listed 'Best First'.
Re: Manipulating multiple matches
by DamnDirtyApe (Curate) on Aug 15, 2002 at 06:54 UTC

    Like this?

    my $string = "&#15123 la di da &#32714 do bi do &#04271" ; print map { chr } $string =~ /&#(\d\d\d\d\d)/g ;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Yes, that works well. However I need to manipulate them (such as you just did) and then replace the match with the newly manipulated part. So that each of the "&#\d\d\d\d\d" becomes chr(\d\d\d\d\d) in the string.
        $string =~ s{&#(\d{5})}{chr( $1 )}ge ;

        _______________
        DamnDirtyApe
        Those who know that they are profound strive for clarity. Those who
        would like to seem profound to the crowd strive for obscurity.
                    --Friedrich Nietzsche
        Try this
        my $string = "&#15123 la di da &#32714 do bi do &#04271" ; $string =~ s/&#(\d\d\d\d\d)/chr($1)/ge ; print "$string\n"
        Hope that helps.
Re: Manipulating multiple matches
by Abigail-II (Bishop) on Aug 15, 2002 at 08:58 UTC
    The code points for ASCII only go from 0 up to 127 inclusive. There are no ASCII characters 15123, 32714, 04271. Perhaps you mean Unicode equivalents, but then the question arises, in which encoding? If you have a modern enough Perl, and you want UTF-8 encoding, chr 32714 etc will give you that. Otherwise, use of one of the Encode modules (which come with perl 5.8) might help you.

    Abigail