Radek Z Radek Z - 1 year ago 56
Python Question

Python: How to find IP adress in text?

tekst= "asdadsasdas asdasdadasdasd"
import re
szukane = r'\d{1,3}.+'
znalezione = re.search(szukane, tekst)

if znalezione:
co= znalezione.group()

I'm looking for IP adress, after run this i got: asdasdadasdasd

What should i change and why?

Answer Source

. is not a literal period in regex. It represents all characters. You'll need to escape it with a backslash. Also, don't forget to include the last group of digits that is not followed by a period.

szukane = r'(?:\d{1,3}\.)+(?:\d{1,3})'


>>> re.findall(r'(?:\d{1,3}\.)+(?:\d{1,3})', "asdadsasdas asdasdadasdasd")
['', '', '']