1

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?

asked May 19, 2015 at 16:20
4
  • possible duplicate of Python Regular Expression example Commented May 19, 2015 at 16:26
  • What are s and its name that you suddenly begin to use? Commented May 19, 2015 at 16:27
  • @MalikBrahimi sorry will update the question Commented May 19, 2015 at 16:28
  • @JoelHinz I dont think they are possible duplicates ... I'm looking for a more general pattern than the one asked in that question Commented May 19, 2015 at 16:31

3 Answers 3

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).

answered May 19, 2015 at 16:33
Sign up to request clarification or add additional context in comments.

1 Comment

This an incorrect regex. Look at the OP where brackets indicated the desired groups in a certain string.
2

Just use the groups that are obtained from the regular expression searched groups:

data = re.search(r'.+\..+\.(.+)\.(.+)-(\d+)', string).groups()
answered May 19, 2015 at 16:35

Comments

0

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.

answered May 19, 2015 at 16:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.