How to use regexp on file, line by line, in Python

后端 未结 7 1920
[愿得一人]
[愿得一人] 2020-12-30 00:24

Here is my regexp: f\\(\\s*([^,]+)\\s*,\\s*([^,]+)\\s*\\)

I\'d have to apply this on a file, line by line. The line by line is OK, simple reading from

7条回答
  •  长发绾君心
    2020-12-30 01:08

    use import re, then re.compile() with your pattern as an argument, and use the resulting object's match attribute on each line. something like this..

    import re 
    pat = re.compile(r'f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)')
    for line in file:
      # use pat.match, pat.search .. etc
    

提交回复
热议问题