简体   繁体   English

使用正则表达式提取字符串

[英]Using regular expression to extract string

I need to extract the IP address from the following string. 我需要从以下字符串中提取IP地址。

>>> 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? 如何在python中使用正则表达式?

No need for regex... Just use str.split 不需要正则表达式...只需使用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): 替代方案(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 常规: \\1.\\2.\\3.\\4

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

Python code Python代码

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 最好的事情是这将匹配,即使实例是ec2ec3所以这个正则表达式实际上非常类似于@mgilson的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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