Question

I have a file in this structure:

009/foo/bar/hi23123/foo/bar231123/foo/bar/yo232131

What i need is to find the exact match of a string; e.g. only /foo/bar among /foo/bar/hi and /foo/bar/yo

One solution came up in my mind is like to check for ending "/" for the input string. Because if there is ending "/" in the possible results, that means it got something different than only /foo/bar.

For this solution, I must say that:

input = /foo/bar

and

match input without ending "/"

How can I do this by Regex in python?

Btw, if there any other solution suggestion, you're welcome to share here.

Was it helpful?

Solution

So you want /foo/bar not followed by a /? If so, then you're looking for a "negative lookahead",

r = re.compile(r'/foo/bar(?!/)')

and then r.search to your heart's content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top