GrayHash
GrayHash

Reputation: 289

pandas regex to extract network IP address substring

How can I find a ip network using regex?

Example

IP 
234.523.213.462:321
21.236.432.123:66666
213.406.421.436:7324

I wanna

IP                Port 
234.523.213.462   321
21.236.432.123    66666
213.406.421.436   7324

Need Help! Thanks.

Upvotes: 2

Views: 1677

Answers (3)

piRSquared
piRSquared

Reputation: 294338

using pd.Series.str.extract

simple regex

df.IP.str.extract('(?P<IP>.+):(?P<Port>\d+)', expand=True)

            IP   Port
0  523.213.462    321
1  236.432.123  66666
2  406.421.436   7324

explicit regex

df.IP.str.extract('(?P<IP>\d{1,3}\.\d{1,3}\.\d{1,3}):(?P<Port>\d+)', expand=True)

            IP   Port
0  523.213.462    321
1  236.432.123  66666
2  406.421.436   7324

Upvotes: 2

jezrael
jezrael

Reputation: 862801

Use vectorize pandas method str.split:

df[['IP','Port']] = df.IP.str.split(':', expand=True)
print (df)
                IP   Port
0  234.523.213.462    321
1   21.236.432.123  66666
2  213.406.421.436   7324

Solution with regex (if there are only numbers, : and .):

df[['IP','Port']] = df.IP.str.extract('(.*):(.*)', expand=True)
print (df)
                IP   Port
0  234.523.213.462    321
1   21.236.432.123  66666
2  213.406.421.436   7324

Upvotes: 5

avysk
avysk

Reputation: 2035

Regex is overcomplication for this task.

In [1]: "213.406.421.436:7324".split(":")
Out[1]: ['213.406.421.436', '7324']

In [2]: "213.406.421.436:7324".split(":")[0]
Out[2]: '213.406.421.436'

In [3]: "213.406.421.436:7324".split(":")[1]
Out[3]: '7324'

You can get both ip and port from your string like this:

ip, port = "213.406.421.436:7324".split(":")

Upvotes: 2

Related Questions