shantanuo
shantanuo

Reputation: 32296

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 e.g.

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?

Upvotes: 3

Views: 133

Answers (3)

Deelaka
Deelaka

Reputation: 13693

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

Regular expression visualization

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

Upvotes: 2

Vasili Syrakis
Vasili Syrakis

Reputation: 9591

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

Upvotes: 2

mgilson
mgilson

Reputation: 309821

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'

Upvotes: 5

Related Questions