Home > Net >  python Regex, filtering items from a list
python Regex, filtering items from a list

Time:03-01

laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i
laptop/k8s-tv-01.ibm.com/tv-i

I want to filter items from a python list.

Format is laptop/k8s-tv-01.ibm.com/tv-And_some_text as you can see that there are 2 forward slashes (/).

What I want to achieve is that whatever comes after the 2nd forward slash which starts from

tv-and_whatever_comes_after should appear.

Currently the code I have, it is only filtering till laptop/k8s-tv-01.ibm.com/tv-iand whatever comes after is not filtering.

my code

```pattern = re.compile(
        r"laptop/k8s-tv-01.ibm.com/tv-[a-zA-Z1-9-]"
    )
    matches = pattern.finditer(stack_loc)
    for match in matches:
        print(match.group())```

CodePudding user response:

Change your regex to this: (?<=laptop/k8s-tv-01\.ibm\.com/)tv-[a-zA-Z1-9-]

import re
stack = "laptop/k8s-tv-01.ibm.com/tv-and-whatever-comes-after"
pattern = re.compile(
        r"(?<=laptop/k8s-tv-01\.ibm\.com/)tv-[a-zA-Z1-9-] "
    )
matches = pattern.finditer(stack)
for match in matches:
    print(match.group())    # tv-and-whatever-comes-after

CodePudding user response:

string = "laptop/k8s-tv-01.ibm.com/tv-i"

match = re.search(r"([^\/] )$", string)

print(match[0])
  • Related