简体   繁体   中英

Using regular expression to extract string

I need to extract the IP address from the following string.

>>> mydns='ec2-54-196-170-182.compute-1.amazonaws.com'

The text to the left of the dot needs to be returned. The following works as expected.

>>> mydns[:18]
'ec2-54-196-170-182'

But it does not work in all cases. For eg

mydns='ec2-666-777-888-999.compute-1.amazonaws.com'

>>> mydns[:18]
'ec2-666-777-888-99'

How to I use regular expressions in python?

No need for regex... Just use str.split

mydns.split('.', 1)[0]

Demo:

>>> mydns='ec2-666-777-888-999.compute-1.amazonaws.com'
>>> mydns.split('.', 1)[0]
'ec2-666-777-888-999'

If you wanted to use regex for this:

Regex String

ec2-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3}).*

Alternative (EC2 Agnostic):

.*\\b([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3}).*

Replacement String

Regular: \\1.\\2.\\3.\\4

Reverse: \\4.\\3.\\2.\\1

Python code

import re
subject = 'ec2-54-196-170-182.compute-1.amazonaws.com'
result = re.sub("ec2-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3}).*", r"\1.\2.\3.\4", subject)

print result

This regex will match (^[^.]+ :

正则表达式可视化

So Try this:

import re

string = "ec2-54-196-170-182.compute-1.amazonaws.com"
ip = re.findall('^[^.]+',string)[0]
print ip

Output:

ec2-54-196-170-182

Best thing is this will match even if the instance was ec2 , ec3 so this regex is actually very much similar to the code of @mgilson

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM