Python| C++ |从 IP 地址中删除前导零
给定一个 IP 地址,从 IP 地址中删除前导零。
例子:
Input : 100.020.003.400
Output : 100.20.3.400
Input :001.200.001.004
Output : 1.200.1.4
方法一:遍历加入
方法是用“.”分割给定的字符串。然后将其转换为删除前导零的整数,然后将它们连接回字符串。要将字符串转换为整数,我们可以使用 int(s),然后通过 str(s) 将其转换回字符串,然后加入他们通过使用加入函数回来。
Python
# Python program to remove leading zeros
# an IP address and print the IP
# function to remove leading zeros
def removeZeros(ip):
# splits the ip by "."
# converts the words to integeres to remove leading removeZeros
# convert back the integer to string and join them back to a string
new_ip = ".".join([str(int(i)) for i in ip.split(".")])
return new_ip ;
# driver code
# example1
ip ="100.020.003.400"
print(removeZeros(ip))
# example2
ip ="001.200.001.004"
print(removeZeros(ip))
C++
// Cpp program to remove leading zeros
// an IP address and print the IP
#include
using namespace std;
// function to remove leading zeros
string removeZeros(string s)
{
vector v;
// splits the ip by "."
for (int i = 0; i < s.length(); i++) {
string ans;
while (i < s.length() && s[i] != '.') {
ans += s[i];
i++;
}
v.push_back(ans);
}
vector num;
// converts the words to integeres to remove leading removeZeros
for (auto str : v) {
int temp = 0;
for (int i = 0; i < str.length(); i++) {
temp *= 10;
temp += (str[i] - '0');
}
num.push_back(temp);
}
string ans = "";
// Convert back the integer to string and join them back to a string
for (auto i : num) {
ans += '.';
string temp;
while (i) {
temp += ('0' + (i % 10));
i /= 10;
}
reverse(temp.begin(), temp.end());
ans += temp;
}
return ans.substr(1);
}
int main()
{
string ip;
// example1
ip = "100.020.003.400";
cout << (removeZeros(ip)) << "\n";
// example2
ip = "001.200.001.004";
cout << (removeZeros(ip)) << "\n";
return 0;
}
Python
# Python program to remove leading zeros
# an IP address and print the IP using regex
import re
# function to remove leading zeros
def removeZeros(ip):
new_ip = re.sub(r'\b0+(\d)', r'\1', ip)
# splits the ip by "."
# converts the words to integeres to remove leading removeZeros
# convert back the integer to string and join them back to a string
return new_ip
# driver code
# example1
ip ="100.020.003.400"
print(removeZeros(ip))
# example2
ip ="001.200.001.004"
print(removeZeros(ip))
输出:
100.20.3.400
1.200.1.4
方法2:正则表达式
使用捕获组,匹配最后一个数字并复制它并防止所有数字被替换。
正则表达式 \d 可以解释为:
- \d :匹配任何十进制数字
\d Matches any decimal digit, this is equivalent
to the set class [0-9].
- \b 允许您使用 \bword\b 形式的正则表达式执行“仅整个单词”搜索
正则表达式 \b 可以解释为:
\b allows you to perform a "whole words only" search u
sing a regular expression in the form of \bword\b
Python
# Python program to remove leading zeros
# an IP address and print the IP using regex
import re
# function to remove leading zeros
def removeZeros(ip):
new_ip = re.sub(r'\b0+(\d)', r'\1', ip)
# splits the ip by "."
# converts the words to integeres to remove leading removeZeros
# convert back the integer to string and join them back to a string
return new_ip
# driver code
# example1
ip ="100.020.003.400"
print(removeZeros(ip))
# example2
ip ="001.200.001.004"
print(removeZeros(ip))
输出:
100.20.3.400
1.200.1.4