試試這個:
reobj = re.compile("^(?P<firstpart>[a-z]+)_(?P<secondpart>[a-z]+)_(?P<lastpart>.*?)$", re.IGNORECASE)
result = reobj.findall(subject)
正則表達式的說明
^(?P<firstpart>[a-z]+)_(?P<secondpart>[a-z]+)_(?P<lastpart>.*?)$
Options: case insensitive
Assert position at the beginning of the string «^»
Match the regular expression below and capture its match into backreference with name 「firstpart」 «(?P<firstpart>[a-z]+)»
Match a single character in the range between 「a」 and 「z」 «[a-z]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character 「_」 literally «_»
Match the regular expression below and capture its match into backreference with name 「secondpart」 «(?P<secondpart>[a-z]+)»
Match a single character in the range between 「a」 and 「z」 «[a-z]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character 「_」 literally «_»
Match the regular expression below and capture its match into backreference with name 「lastpart」 «(?P<lastpart>.*?)»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
一些澄清會很好。 's'中的最後一個下劃線在o和w之間,在's1'中,在d和f之間,在s2中,在o和b之間(但是你後來說在第一個下劃線之後要'串' o和w之間的一個)。看起來你的「串」和「捕捉」的含義與我所擁有的不同。你能詳細說明你到底想要什麼嗎? – Jerry