다음 파이썬 코드를 사용하여 작업 : 정규식 lookbehind하지 re.match
import re
line="http://google.com"
procLine = re.match(r'(?<=http).*', line)
if procLine.group() == "":
print(line + ": did not match regex")
else:
print(procLine.group())
이 성공적으로 일치하지 않는 경우, 다음과 같은 오류 출력 : 난 그냥와 정규식을 교체 할 때
Traceback (most recent call last): File "C:/Users/myUser/Documents/myScript.py", line 5, in if procLine.group() == "": AttributeError: 'NoneType' object has no attribute 'group'
합니다. * 괜찮 았던 것을 나타내는 정규식을 제안하는 것은 괜찮습니다. 그러나 https://regex101.com/에서 정규식과 문자열을 파이썬 맛을 테스트 할 때 잘 일치하는 것처럼 보입니다.
아이디어가 있으십니까? 당신이 비 캡처 그룹에 lookbehind를 변환하면
당신은 대신'search'를 사용 ([문서] https://docs.python.org/을 확인 할 수 있습니다 3/library/re.html # regular-expression-syntax) : "긍정적 인 lookbehind 어설 션으로 시작하는 패턴은 검색되는 문자열의 시작 부분에서 일치하지 않으므로 search() 함수를 사용하는 것이 좋습니다() 함수보다 " – niemmi