How to slice string considering both ends are exclusive using regex?

Viewed 53

I have a string output like this-

KActive[m[K: active (running) since Sat 2021-07-29 00:11:50 IST; 8h ago', {'command': 'service tmi status | grep Active'}

I want to cut it from active to ago. Final output should be-

active (running) since Sat 2021-07-29 00:11:50 IST; 8h ago

What is the best way to slice it in python? i am not looking for string_name[start_index:stop_index] rather I would like to know how to slice it using regex and python.

1 Answers

Using the regex provided by Cary Swoveland you can do this easily with re.search().

import re

input_string = "KActive[m[K: active (running) since Sat 2021-07-29 00:11:50 IST; 8h ago', {'command': 'service tmi status | grep Active'}"

def get_status(string):
    match = re.search(r'\bstatus .*? ago\b', string)
    match = match.group(0)
    return match

print(get_status(input_string))
Related