2

I'm trying to scan an Amazon S3 bucket, in order to search if a new version of our installer has been posted:

The bucket scan returns me something like:

versions/
versions/4.4.1.2/
versions/4.4.1.2/Installer.sh
versions/4.4.2.11/
versions/4.4.2.11/Installer.sh
versions/4.5.0.10a/
versions/4.5.0.10a/Installer.sh
versions/4.5.0.12a/
versions/4.5.0.12a/Installer.sh

I only need to get the <{d}.{d}.{d}.{d}{a-z]}> part between versions ... how do I parse that using regex in Python?

3
  • What is your expected output from the above?
    – hwnd
    Commented Oct 2, 2014 at 23:17
  • line.split('/')[1] ?
    – Kent
    Commented Oct 2, 2014 at 23:18
  • try:line.split ('/')[1] except: pass Commented Oct 2, 2014 at 23:52

3 Answers 3

2

Use re module:

import re
text = "versions/4.5.0.10a/"
re.search('\d+\.\d+\.\d+\.\d+[a-z]', text).group()
Output: '4.5.0.10a'
1
("versions/4.4.1.2/").split("/")[1]
1

You could do this through Positive lookahead and lookbehind.

>>> s = "versions/";
>>> m = re.compile(r'(?<=/).*?(?=/)')
>>> re.search(m, s)
>>> s = "versions/4.4.1.2/";
>>> re.search(m, s).group()
'4.4.1.2'
>>> s = "versions/4.4.1.2/Installer.sh";
>>> re.search(m, s).group()
'4.4.1.2'

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.