我想在比賽前后得到單詞。我可以使用string.split(' ')- 但由于我已經(jīng)使用了正則表達式,難道沒有更好的方法只使用正則表達式嗎?使用匹配對象,我可以獲得確切的位置。但是,此位置是字符索引的。import remyString = "this. is 12my90\nExample string"pattern = re.compile(r"(\b12(\w+)90\b)",re.IGNORECASE | re.UNICODE)m = pattern.search(myString)print("Hit: "+m.group())print("Indix range: "+str(m.span()))print("Words around match: "+myString[m.start()-1:m.end()+1]) # should be +/-1 in _words_, not characters輸出:命中:12my90 指數(shù)范圍:(9, 15)比賽周圍的話:12my90為了獲得匹配的單詞和之前的單詞,我嘗試了:pattern = re.compile(r"(\b(w+)\b)\s(\b12(\w+)90\b)",re.IGNORECASE | re.UNICODE)這不會產(chǎn)生匹配項。
添加回答
舉報
0/150
提交
取消
