0

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.

1
  • 1
    Do you not know how to use the Python regular expression methods or do you not know how to use regular expressions in general? Commented Oct 17, 2009 at 18:37

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.