kitty
kitty

Reputation: 105

endswith() matching?

I am using the code below to match using endswith in Python.

while not buff.endswith('/abc #'):

But I run into a problem when the line ends as shown below:

('console /abc/xyz* #')

('console /abc/xyz/pqrs* #')

Now how to match with endswith for "/abc" anywhere, but # must be the last character?

Upvotes: 0

Views: 1225

Answers (2)

user94559
user94559

Reputation: 60143

I think there's a language barrier here making it hard to understand the requirements, so I'll just propose a solution based on my best guess:

while not (buff.endswith("#") and "/abc" in buff):

Upvotes: 2

import random
import random

Reputation: 3245

If the number of paths you're trying to match is small, you can supply a tuple to the endswith method.

>>> buffs = ["console /abc #", "console /abc/d #", "console /abc/xyz/* #"]
>>> for b in buffs: print(b.endswith("#"))
...
True
True
True
>>> for b in buffs: print(b.endswith("/abc #"))
...
True
False
False
>>> for b in buffs: print(b.endswith(("/abc #", "/abc/d #")))
...
True
True
False
>>>

Otherwise you'll need to use a regular expression for your search, e.g.

>>> import re
>>> p = re.compile(r"/abc.*#$")  # match "/abc", anything, "#", then EoL
>>> for b in buffs: print(p.search(b) != None)
...
True
True
True

Upvotes: 0

Related Questions