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

后端 未结 7 1919
[愿得一人]
[愿得一人] 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 01:08

    import re
    with open('file.txt') as f:
        for line in f:
            match = re.search('f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)', line)
    

    Note that Python automatically compiles and caches the regex, so a separate compile step is not required in this case.

提交回复
热议问题