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.
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
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
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(":")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.