Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with parsing using re
#3
It will work if you change what @ichabod801 posted.

Here to show other way with re.finditer()
Taken read()(all to string) and findall()(all to list) out of picture.
Now only line bye line get read to memory.
This can be useful if your file was bigger that this.
import re

with open('test.txt') as f,open('ip.txt','w') as f_out:
    for line in f:
        ip = re.compile(r'\d+\.\d+\.\d+\.\d+')
        for match in ip.finditer(line):
            f_out.write(f'{match.group()}\n')
Output:
192.168.1.108 192.168.1.105 192.168.1.102 192.168.1.101 192.168.1.100 192.168.1.10
Reply


Messages In This Thread
Help with parsing using re - by ggj13557 - Jul-14-2018, 03:58 PM
RE: Help with parsing using re - by ichabod801 - Jul-14-2018, 05:14 PM
RE: Help with parsing using re - by snippsat - Jul-14-2018, 05:50 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020