http://qs321.pair.com?node_id=768792

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

my $line = "<ci:Name> Lord: Of The $10Ring$s </ci:Name>"; Here I have to delete the dollar with it doesnot preceed with digit and not within the square brackets

Replies are listed 'Best First'.
Re: Regular expression
by JavaFan (Canon) on Jun 05, 2009 at 14:34 UTC
    I presume that your brackets cannot be nested? Because in your example strong, all the dollars are between brackets. (Note also that as is, we cannot deduce there are dollar signs in $line, as $10 and $s are two interpolated variables. But let's assume there's a lone q before the first double quote).

    Assume that the above is true, and that all brackets are balanced, I'd write something like:

    { no warnings 'uninitialized'; $line =~ s/(<[^>]*>|\$[0-9])|\$/$1/g; }
    Alternatively, keep the warning, replace the replacement with $1 || "" and use /e to eval the replacement.
      Re:Because in your example strong [sic], all the dollars are between brackets.

      I don't see any square brackets in his post, and the string is not inside the angle brackets that form the tags.

        I presumed he meant the < and > characters when he mentioned "square brackets" - otherwise the example doesn't make much sense.

        And considering the string starts with < and ends with >, the "between brackets" condition was ambigious. I just pointed out which meaning I was going to assume. It seems that is the same meaning as you assigned to it.

Re: Regular expression
by targetsmart (Curate) on Jun 05, 2009 at 13:28 UTC
    if I have understood you correctly, this will do
    $line =~ s/\$\d+|\$|<.*?>//g;
    will make $line to
    Lord: Of The Rings
    BTW perlretut is your friend.
    Please form your question correctly, I am not clear with your question. :(

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      The OP only wants to delete dollars that aren't inside brackets, and aren't followed by digits. You delete everything between brackets (including the brackets), and any following digits.
Re: Regular expression
by Anonymous Monk on Jun 05, 2009 at 14:04 UTC