简体   繁体   中英

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

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. write [pos+1:] what will give you pos to the end of the string.

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] . 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() :

val = "Active Scanning: Scanning IP Blocks"

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

print(val)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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