in reply to Replace not working

Thanks a lot everyone. G and Y in the example could be any chars. I have a question for ig. In your post you replied that the literal '-' in -[^\s-~] represents one char and the char set represented another char and that is the reason why two chars was replaced. But the set contains a ^ symbol which tells system not to consider the chars given in the set. So could you let me know exactly what happens in the above stmnt. Thanks.

Replies are listed 'Best First'.
Re^2: Replace not working
by AnomalousMonk (Archbishop) on Aug 09, 2009 at 20:21 UTC
    The  ^ regex metacharacter at the very beginning of a character set or class complements the characters defined for the set.

    The character class regex  [^\s-~] matches any character that is not a whitespace character, a literal '-' (dash or hyphen) or a literal '~' (tilde). A 'Y' is not any of those characters, so it matches.

    BTW — the character set  [^\s-~] is better written as  [^-\s~] so that the '-' character is not misunderstood (by the programmer or maintainer, not by Perl) as a character set range metacharacter. I'm guessing you are not running with warnings (and strictures), otherwise Perl would have complained about this:

    >perl -wMstrict -le "my $rx = qr{ [^\s-~] }xms; print $rx;" False [] range "\s-" in regex; marked by <-- HERE in m/ [^\s- <-- HERE ~] / at -e line 1. (?msx-i: [^\s-~] )
Re^2: Replace not working
by graff (Chancellor) on Aug 10, 2009 at 04:04 UTC
    You said:

    G and Y in the example could be any chars.

    Well, in case your code is still not doing exactly what you want, and you're still wondering how to fix it, maybe it's time to provide some of the real examples that you are actually dealing with.

    I have to say that the example string in the OP is probably not what you meant it to be. In this statement:

    $second_str = "591[\s-~]*G-Y[\s-~]*6";
    the value assigned to "$second_str" is actually:
    591[s-~]*G-Y[s-~]*6
    Do you really have literal strings in your data that look like that? If you really do, is there some special rule about hyphens that are inside square brackets (don't replace with space) vs. hyphens that are not inside square brackets (replace these with space)?

    If it's just a matter of replacing all hyphens with spaces, then this will do it more efficiently:

    tr/-/ /;
    But if you have contextual constraints (e.g. must be adjacent to letters/alphanumerics, and/or must not be within bracketed strings, or whatever), you haven't really given us any clear idea about that yet.