Raw Input
keep 123456789 but change 12345678 and 12481632
keep abc12345678 but change 23581113 and 36912150
Desired Output
keep 123456789 but change ABCDEFGH and ABCDEFGH
keep abc12345678 but change ABCDEFGH and ABCDEFGH
Script and Comments
Script1 [perl]
[ 1] s/(?<!abc)(?<!\d)\d{8}(?!\d)/ABCDEFGH/g
Comments
  1. An 8-digit integer must not be followed nor preceded by any digit character.
  2. To match any 8-digit integer, we can use (?!<\d)\d{8}(?!\d), where:
    • \d is equivalent to [0-9], can be used to match any digit character.
    • (?!<\d) means that the following RE must not be preceded by any digit character. where (?!<...) is the negative look-behind
    • operator of Perl.
    • (?!\d) means that the preceding RE must not be followed by any digit character, where (?!...) is the negative look-ahead operator of Perl.
    To replace every 8-digit integer with 'ABCDEFGH',
    we can use s/(?<!\d)\d{8}(?!\d)/ABCDEFGH/g
  3. Another approach is s/(^|\D)\d{8}(?!\d)/${1}ABCDEFGH/g,
    where (^|\D) is used to enforce that \d{8} is either at the start of a line or preceded by one non-digit character.
  4. To replace every 8-digit integer not preceded by 'abc' with 'ABCDEFGH', we can use the look-behind operator twice:
    s/(?<!abc)(?<!\d)\d{8}(?!\d)/ABCDEFGH/g.