简体   繁体   English

如何从最后一个“。”之后的字符串中删除最后一个字符

[英]How do i remove the last character from a string after the last “.”

Im working on a python scipt which gets and Ip and calculates the brodcast which just removes the last digest of the ip after the last "."我正在研究一个 python scipt,它获取和 Ip 并计算在最后一个“”之后删除 ip 的最后一个摘要的广播。 and replaces them with 255并将它们替换为 255

input = "192.168.0.100" 
input = #string operation
print(input)
input = "192.168.0.100" 
loc = input.rfind('.')
inputShort = input[1:loc]
print(loc)
print(inputShort)

rfind finds the last incidence of the "." rfind查找“。”的最后一次出现。 character and inputShort is then all characters up until, but excluding the last ".". character 和inputShort然后是所有字符,但不包括最后一个“.”。

Broadcast address is not always going to be just 255 as last digest.广播地址并不总是只有 255 作为最后一个摘要。 That would be true only if the prefix len is /24 (or mask is 255.255.255.0).仅当前缀 len 为 /24(或 mask 为 255.255.255.0)时才会如此。

>>> input = "192.168.0.100"
>>> from re import sub
>>> sub('(.[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-4])$', '.255', input)
'192.168.5.255'

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

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