python 补前导零_Python正则表达式| 程序从IP地址中删除前导零
python 補前導零
Given an IP address as input, write a Python program to remove leading zeros from it.
給定一個IP地址作為輸入,編寫一個Python程序以從中刪除前導零。
Examples:
例子:
Input: 216.08.094.196Output: 216.8.94.196Input: 216.08.004.096Output: 216.8.4.96In this program, we are using sub() method of "re" module.
在此程序中,我們使用“ re”模塊的 sub()方法 。
Syntax:
句法:
re.sub(pattern, repl, string, count=0, flags=0)The sub() in the function stands for SubString, a certain regular expression pattern is searched in the given string(3rd parameter), and upon finding the substring pattern is replaced by repl(2nd parameter), count checks and maintains the number of times this occurs.
在功能子()代表子串,一個特定正則表達式模式中搜索給定的字符串( 第三參數)中,并在找到的子圖案由REPL(第2參數)代替,計數檢查和維護數這種情況經常發生。
Code
碼
# Python program to Remove leading zeros from an IP address# import re module# re module provides support # for regular expressions import re# Make a regular expression for # finding leading zeros in ip address regex = '\.[0]*'# Define a function for Remove # leading zeros from an IP address def removeLeadingZeros(ip):modified_ip = re.sub(regex, '.', ip)print(modified_ip)# Main code if __name__ == '__main__' : # Enter ip address ip = "216.08.094.196"# call function removeLeadingZeros(ip)ip = "216.08.004.096"removeLeadingZeros(ip)Output
輸出量
216.8.94.196 216.8.4.96翻譯自: https://www.includehelp.com/python/python-regex-program-to-remove-leading-zeros-from-an-ip-address.aspx
python 補前導零
總結
以上是生活随笔為你收集整理的python 补前导零_Python正则表达式| 程序从IP地址中删除前导零的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java LocalDate类| isS
- 下一篇: perl 哈希数组的哈希_使用哈希检查两