I am trying to parse a string using regex which is in particular format to get details out of it. I can have my string in two formats -
First format
One way is to have a foldername-version.tgz. Here foldername can be any string in any format. It can have another or multiple - in it or anything else.
For example:
- hello-1234.tgz: This should give me
FolderNameashelloandVersionas1234 - world-12345.tgz: This should give me
FolderNameasworldandVersionas12345 - hello-21234-12345.tgz: This should give me
FolderNameashello-21234andVersionas12345 - hello-21234-a-12345.tgz: This should give me
FolderNameashello-21234-aandVersionas12345
Second format
Other way is to have foldername-version-environment.tgz. In this case also foldername can be any string in any format. Also environment string can only be dev, stage, prod and nothing else so I need to add check on that as well.
For example:
- hello-1234-dev.tgz: This should give me
FolderNameashelloandVersionas1234 - world-12345-stage.tgz: This should give me
FolderNameasworldandVersionas12345 - hello-21234-12345-prod.tgz: This should give me
FolderNameashello-21234andVersionas12345 - hello-21234-a-12345-prod.tgz: This should give me
FolderNameashello-21234-aandVersionas12345
Problem Statement
So with the above two format - I need to extract FolderName and Version from my string. I tried with below regex but it doesn't work on my strings which are in second format but I want my code to work on both the formats.
#sample example string which can be in first or second format
exampleString = hello-21234-12345-prod.tgz
build_found = re.search(r'[\d.-]+.tgz', exampleString)
version = build_found.group().replace(".tgz", "")
folderName = exampleString.split(version)[0]
What is wrong I am doing here?