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

Hi, maybe I'm simply too stupied but I've search and search now for 2 days and didn't came to a conclusion. I have to subsitute the following text in an ascii file:
my $string = "aDescriptor.Type = {\@see Star.Awt.Win#TOP}"; $string =~ s/ {\@see\s+((\w+\.)+)(\w+)#(\w+)}/ <idl package="$1" name="$3" anker="$4" /> /g; # desired output would be : # aDescriptor.Type = <idl package="star.awt." name="Win" anker="TOP"/>
The regular expression is already working but I could not manage to convert the the package to lower case.

Replies are listed 'Best First'.
Re: Regexp matched part to lower case
by monarch (Priest) on Jun 02, 2005 at 07:23 UTC
    The following code:
    use strict; use warnings; my $string = "aDescriptor.Type = {\@see Star.Awt.Win#TOP}"; print( "Original:$string\n" ); $string =~ s/ {\@see\s+((\w+\.)+)(\w+)\#(\w+)} / "<idl package=\"" . lc($1) . "\" name=\"$3\" anker=\"$4\">" /gex; print( "Updated:$string\n" );

    ..produces the following output..

    Original:aDescriptor.Type = {@see Star.Awt.Win#TOP} Updated:aDescriptor.Type = <idl package="star.awt." name="Win" anker=" +TOP">

    Update: holli has the best answer below. I suspected there was an in-place capitalisation modifier, and clearly this is the best solution.

      There's no need for the /e mofier.
      my $string = "aDescriptor.Type = {\@see Star.Awt.Win#TOP}"; $string =~ s/{\@see\s+((\w+\.)+)(\w+)#(\w+)}/<idl package="\L$1\E" nam +e="$3" anker="$4" \/>/g;


      holli, /regexed monk/
Re: Regexp matched part to lower case
by prasadbabu (Prior) on Jun 02, 2005 at 07:11 UTC

    You can make use of lc() function to change into lower case and add 'e' option modifier.

    Prasad

      You think like this? $string =~ s/{\@see\s+((\w+\.)+)(\w+)#(\w+)}/<idl package="lc($1)" name="$3" anker="$4"/>/eg; But that's not working ;-)
        the right hand side will be evaluated. Thus, treat it not as surrounded by quotes, but as an expression.

        like:

        s/.../"<idl package=\"".lc($1)."\" name=\"$3\" anker=\"$4\"\/>"/ge

        Update: sorry, forgot the "'s

        Here \ is a special character before @ so you have to escape it by \\.

        Then do slight change in replacement part.

        /"<idl package=\"".lc($1)."\" name=\"$3\" anker=\"$4\"\/>"/esi;

        updated

        Prasad

Re: Regexp matched part to lower case
by PodMaster (Abbot) on Jun 02, 2005 at 08:54 UTC
    Those in the know use (:
    die "\LBLAH BLAH"
    `perldoc perlop'

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.