简体   繁体   中英

Parsing a string pattern - Python

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?

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

只需使用groups ,从正则表达式搜索组获得:

data = re.search(r'.+\..+\.(.+)\.(.+)-(\d+)', string).groups()

If you strictly want to pull out the testset and testcase, ie "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM