Python regex parse stream

前端 未结 4 693
失恋的感觉
失恋的感觉 2020-12-09 15:03

Is there any way to use regex match on a stream in python? like

reg = re.compile(r\'\\w+\')
reg.match(StringIO.StringIO(\'aa aaa aa\'))

And

4条回答
  •  被撕碎了的回忆
    2020-12-09 16:09

    Yes - using the getvalue method:

    import cStringIO
    import re
    
    data = cStringIO.StringIO("some text")
    regex = re.compile(r"\w+")
    regex.match(data.getvalue())
    

提交回复
热议问题