简体   繁体   English

删除之前的所有内容:

[英]Remove everything from before the :

I need to remove everything before the :我需要删除之前的所有内容:

Active Scanning: Scanning IP Blocks

I just need to keep the Scanning IP Blocks我只需要保留Scanning IP Blocks

val = "Active Scanning: Scanning IP Blocks"

pos = val.rfind(':')
if pos >= 0:
    val = val[:pos]

print(val)

But I'm getting the everything before the :但我得到了之前的一切:

[:pos] means 0 to pos which you don't want. [:pos]表示0到您不想要的pos write [pos+1:] what will give you pos to the end of the string.[pos+1:]什么会给你pos到字符串的末尾。

val = "Active Scanning: Scanning IP Blocks"

pos = val.rfind(':')
if pos >= 0:
    val = val[pos+1:]

print(val)

You can try:你可以试试:

>>> import re
>>> val = "Active Scanning: Scanning IP Blocks"
>>> re.sub(r'^.*?:', '', val)
' Scanning IP Blocks'

The syntax for string indexing is string[startIndex:endIndex] .字符串索引的语法是string[startIndex:endIndex] You can use:您可以使用:

val = "Active Scanning: Scanning IP Blocks"

pos = val.rfind(':')
if pos >= 0:
    val = val[pos+1:]

print(val)

or you can use split() :或者您可以使用split()

val = "Active Scanning: Scanning IP Blocks"

pos = val.rfind(':')
if pos >= 0:
    val = val.split(":")[1]

print(val)

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

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