in reply to Replacing characters in a line

Hi, just try this,

$string='ATOM 3316 N TYR D 24 97.479 18.805 2.990 1.00 +36.32 D N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + D C'; $string=~s#((\w+) \d{4} \w TYR )D#$1X#gsi; print $string; output: ******* ATOM 3316 N TYR X 24 97.479 18.805 2.990 1.00 36.32 + D N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + D C

Replies are listed 'Best First'.
Re^2: Replacing characters in a line
by holli (Abbot) on Mar 04, 2005 at 12:45 UTC
    Why not just s/(?<=TYR )D/X/;?

    Update:
    Or, to keep it fixed-length:
    s/^(.{21})D/$1X/;

    Sidenote:
    Has anyone an idea, why this:
    s/^(?<=.{21})D/X/;
    does not work, while this
    s/(?<=.{21})D/X/;
    does (but donīt use this, itīs unsafe)?


    holli, /regexed monk/
      Are we sure that D will always be preceeded by "TYR " ??
      Manav
        We can only tell from the example data.
        pelagic's regex is also unsafe. what if the line looks like
        ATOM 3316 N TYR B 24 97.479 18.805 2.990 1.00 36.32 + D N
        Then the alone "D" at the end will be replaced by "X". Intended??


        I would use the substr()-solution above. My regex was just meant as an optimization of perlsen's one, which is unneccesary complicated.

        Update:
        Striked the sillyness about the second "D".
        /me writes "Must read the question properly" 200 times.


        holli, /regexed monk/
        We are not!
        As the OP stated: there are two D's on every line.

        pelagic