re module (help with backslashes)

Trent Mick trentm at ActiveState.com
Wed Feb 5 18:16:16 EST 2003


[Stephen Boulet wrote]
> I want to match 2 to 18 occurrences of a backslash plus any 3 alphanumeric 
> characters. The goal is to replace contiguous blocks of backslash + 3 
> alphanumerics with a tab.
> 


    import re

    s='J1Z0\\000\\000\\000\\000\\005\\352\\275\\360\\000\\000\\000\\000\\000\\000\\000\\012UUT_0\\000\\000\\000\\005\\352\\275\\360\\000\\000\\000\\000\\000\\000\\000\\0102_V\\000\\000\\000\\000\\000\\005\\352\\275\\360\\000\\000\\000\\000\\000\\000\\000\\007Ch1=11.2V_Ch2=0V_Ch3=0V\\000\\006\\024\\350\\320\\000JP\\350\\006\\024\\212\\340\\006\\024\\230\\210_degrees_C_RH\\000\\000\\000\\005\\352\\275\\360\\000\\000\\000\\000\\000\\000\\000\\007\n\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\0006_V\\000\\000\\000\\000\\000\\005\\352\\275\\360\\000\\000\\000\\000\\000\\000\\000\\007Ch1=11.8V_Ch2=0V_Ch3=0V\\000\\006\\024\\350\\320\\000JP\\350\\006\\024\\212\\340\\006\\024\\230\\210\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\n\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\00\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\00010_V\\000\\000\\000\\000\\005\\352\\275\\360\\000\\000\\000\\000\\000\\000\\000\\007Ch1=12.4V_Ch2=0V_Ch3=0V\\000\\006\\024\\350\\320\\000JP\\350\\006\\024\\212\\340\\006\\024\\230\\210\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\n\n\n'

    p = re.compile(r"(\\\w\w\w){2,18}")
    print p.sub("\t", s)


This works for me.

Cheers,
Trent


-- 
Trent Mick
TrentM at ActiveState.com





More information about the Python-list mailing list