-1

I am looking for a way to return a boolean result for a column finding ip addresses in a url as true and no ip address in url as false

    col1
0   http://197.248.5.23/
1   https://197.248.5.23/
2   http://179.12.170.88/pdf/forced/jit/
3   http://tel:888
4   http://li

Desired output:

    col1                                   contains_ip
0   http://197.248.5.23/                   True
1   https://197.248.5.23/                  True
2   http://179.12.170.88/pdf/forced/jit/   True
3   http://tel:888                         False
4   http://li                              False
sectechguy
  • 2,037
  • 4
  • 28
  • 61
  • 1
    `df['col1'].str.contains('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')` inspired from here: https://stackoverflow.com/questions/3462784/check-if-a-string-matches-an-ip-address-pattern-in-python also check the accepted answer – anky Sep 17 '19 at 14:45

2 Answers2

2

You could try this regex:

df.col1.str.match('^(http|https)://\d+\.\d+\.\d+\.\d+\.*')

Output:

0     True
1     True
2     True
3    False
4    False
Name: col1, dtype: bool
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
1
df["col1"].str.match(r"https?://(\d+\.){3}\d+/")
Dev Khadka
  • 5,142
  • 4
  • 19
  • 33