简体   繁体   中英

Find string after “task-” in a long substring using regex

I have list of files with a pattern sub-*_task-XYZabc_run-*_bold.json and sub-*_task-PQRghu_bold.json , for example:

sub-03_task-dis_run-01_bold.json
sub-03_task-dis_run-02_bold.json
sub-03_task-dis_run-03_bold.json
sub-03_task-dis_run-04_bold.json
sub-03_task-dis_run-05_bold.json
sub-03_task-dis_run-06_bold.json
sub-03_task-fb_run-01_bold.json
sub-03_task-fb_run-02_bold.json
sub-03_task-fb_run-03_bold.json
sub-03_task-fb_run-04_bold.json

I intend to find all different task names from the filename. In the above example, dis and fb are the two tasks.

What kind of regex should I use to find TASKNAME from task-TASKNAME in a given filename?

The following regex should do it :

(?<=task-).*?(?=_)

see regex demo / explanation

python ( demo )

import re
regex = r"(?<=task-).*?(?=_)"
str = """sub-03_task-dis_run-01_bold.json
         sub-03_task-dis_run-02_bold.json
         sub-03_task-dis_run-03_bold.json
         sub-03_task-dis_run-04_bold.json
         sub-03_task-dis_run-05_bold.json
         sub-03_task-dis_run-06_bold.json
         sub-03_task-fb_run-01_bold.json
         sub-03_task-fb_run-02_bold.json
         sub-03_task-fb_run-03_bold.json
         sub-03_task-fb_run-04_bold.json"""
matches = re.finditer(regex, str)
for matchNum, match in enumerate(matches):
    matchNum = matchNum + 1
    print ("{match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

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