예 : 네덜란드에서 땅콩 버터는 "pindaboter"(땅콩 버터)가 아니라 "pindakaas"(땅콩 치즈)라고합니다. 버터라는 단어는 실제 버터를 포함하는 제품에만 사용해야하기 때문입니다.
나는 사이의 모든 일치시킬 cheese
및 butter
및 viceversa에 있습니다.
목표 :
편집 : 사용 된 언어는 Python 3.7이고 현재 사용중인 정규식은 cheese(.*?)butter
.
저장소 에서 regex
패키지 를 설치하는 경우 검색 PyPI
을 수행 할 수 있습니다 overlapped
.
import regex as re
text = 'In the Netherlands, peanut butter is called "pindakaas" (peanut cheese) rather than "pindaboter" (peanut butter) because the word butter is only supposed to be used with products that contain actual butter.'
l = re.findall(r'\bbutter\b.*?\bcheese\b|\bcheese\b.*?\bbutter\b', text, overlapped=True)
print(l)
인쇄물:
['butter is called "pindakaas" (peanut cheese', 'cheese) rather than "pindaboter" (peanut butter']
나는 당신의 기본 정규식을 사용하지만, 필요 butter
및 cheese
예를 들어 워드 경계에있을 \bbutter\b
배치하여, \b
이전과 단어 후. 제거 여부를 부담없이 사용하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다