I have a string pattern (for a xml test reporter) in the following pattern:
'testsets.testcases.[testset].[testcase]-[date-stamp]'
For example:
a='testsets.testcases.test_different_blob_sizes.TestDifferentBlobSizes-20150430130436'
I know I always can parse the testset and testcase names by doing:
temp = a.split("-")[0]
current = temp.split(".")
testset = '.'.join(current[:-1]) + ".py"
testcase = current[-1]
However, I want to accomplish that using a more pythonic way, like regex or any other expression that I would do it in a single line. How can I accomplish that?
3 Answers 3
You can try:
testset, testcase = re.search('(.*)\.(.*)-.*', a).group(1, 2)
testset += '.py'
re.search returns a MatchObject on matches, and it has a group method we can use to extract match groups for the regex ("()"s in the regex).
1 Comment
Just use the groups that are obtained from the regular expression searched groups:
data = re.search(r'.+\..+\.(.+)\.(.+)-(\d+)', string).groups()
Comments
If you strictly want to pull out the testset and testcase, i.e. "test_different_blob_sizes" and "TestDifferentBlobSizes", as in the first part of your question, you can just do:
testset, testcase = re.split('[.-]',s)[2:4]
For compact regexp-based code based on what you have, see Ziyao Wei's response.
sand itsnamethat you suddenly begin to use?