2013-06-23 4 views
2

LDAP 항목이 여러 개인 로그 파일이 있는데 특정 날짜에 createtimestamp가있는 항목 만 일치 시키려고하지만 타임 스탬프가 아닌 전체 항목을 캡처하려고합니다. 항목은 다음과 같습니다.LDAP 항목을 여러 줄 정규식으로 일치시키는 데 문제가 있습니다.

dn: .... 
otherattr: 
... 
createtimestamp: 20130621061525Z 

문제는 제가 원했던 모든 항목을 가져 오는 것입니다.

dn_search = re.compile(r'dn: (.*?)createtimestamp: 20130[4-6]\d+?Z', flags=re.M|re.S) 

내가 다른 표현을 시도했지만 나도에만 createtimestamp 또는 원치 않는 항목을 얻고있다 :

dn: .... 
otherattr: 
... 
createtimestamp: 20121221082545Z 

dn: .... 
otherattr: 
... 
createtimestamp: 20130621061525Z 

는 표현이다. 어떤 아이디어?

+1

날짜를 datetime 개체로 파싱 한 다음 필터링하는 이유는 무엇입니까? – Ben

+1

@Ben Yea, 그건 효과가있었습니다. 방금 정규식을 머리에 집어 넣었던 것 같아요. 감사. – Adam

답변

2

이 정규식은 텍스트의 각 그룹 dn: 시작 가정하고 빈 줄로 끝나는 것

설명. 그런 다음

링크 예를 http://repl.it/J0t

코드

작업에 createtimestamp 필드의 값

^dn:(?=(?:(?!^createtimestamp:|^dn:|^\s*(?:\r|\n\|$)|\Z).)*^createtimestamp:\s*([^\s\r\n]*))(?:(?!^dn:|^\s*(?:\r|\n\|$)|\Z).)*

enter image description here

파이썬 코드 예제를 라인의 전체 그룹을 캡처하고 캡처
import re 

string = """dn: .... 
otherattr: 
... 
createtimestamp: 20121221082545Z_1 

dn: .... 
otherattr: 
... 
createtimestamp: 20130621061525Z_2 
"""; 

for matchObj in re.finditer(r'^dn:(?=(?:(?!^createtimestamp:|^dn:|^\s*(?:\r|\n\|$)|\Z).)*^createtimestamp:\s*([^\s\r\n]*))(?:(?!^dn:|^\s*(?:\r|\n\|$)|\Z).)*', string, re.M|re.I|re.S): 
    print "-------" 
    print "matchObj.group(1) : ", matchObj.group(1) 

반환

------- 
matchObj.group(1) : 20121221082545Z_1 
------- 
matchObj.group(1) : 20130621061525Z_2 

+0

파이썬에서는 작동하지 않는 것 같지만 정보는 고마워요. – Adam

+0

이상하게도 PHP에서는 작동하지만 파이썬에서는 작동하지 않습니다. 오. 업데이트를 참조하십시오. –

2

손으로 LDIF 구문 분석하려고하지 마십시오. 복잡한 것은 아니지만 속성과 이름 이스케이프, 긴 행의 연속성과 같은 것들이 당신을 물들 일 것입니다. 을 사용하십시오.

+0

python-ldap에 대한 정보를 제공해 주셔서 감사합니다. – Adam

관련 문제