Python str.strip() with regex filtering unexpected characters

前端 未结 6 823
广开言路
广开言路 2020-12-19 22:58

I\'m running into an issue that I hope is simple, however I\'ve run into a wall trying to figure it out. I\'m attempting to strip the DateTime timestamp from the beginning

6条回答
  •  旧时难觅i
    2020-12-19 23:22

    b is '[Wed Dec 01 10:24:24 2010]' so then you strip any of the characters that are in b from c so everything bar ct get removed:

    '[Wed Dec 01 10:24:24 2010] ceeeeest'
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   
     # all in [Wed Dec 01 10:24:24 2010]
    

    So only st remain as they are the only two characters no in b, strip will keep stripping from both ends until it hits char not in the set:

    In [3]: s = "fooboaroof"
    
    In [4]: s.strip("foo")
    Out[4]: 'boar'
    

    If the date is always at the start which it must be if you are using match, when you get a match the simplest would be to split:

    line2 = '[Wed Dec 01 10:24:24 2010] ceeeeest'
    
    print(line2.split("] ", 1)[1])
    

    Or:

     print(line2[len(a.group()):].lstrip())
    

提交回复
热议问题