2013-02-22 1 views
1

두 개의 조건 ProfileBuildDate에 대한 간단한 정규식 검색을 실행하고 있습니다. 가능한 한 많은 정보를 반환하거나 결과를 하나 또는 둘 이상 찾을 수있는 가능성을 말하고 싶습니다. 내가 쓰는 방법은 여기에있다. 그러나 파이썬 적 방법이 더 있는지 궁금하다. 또한파이썬 정규식 검색에서 일치하는 항목을 반환하는 더 나은 방법은 무엇입니까?

p = re.search(r'Profile\t+(\w+)',s) 
    d = re.search(r'BuildDate\t+([A-z0-9-]+)',s) 

    # Return whatever you can find. 
    if p is None and d is None: 
     return (None, None) 
    elif p is None: 
     return (None, d.group(1)) 
    elif d is None: 
     return (p.group(1), None) 
    else: 
     return (p.group(1),d.group(1)) 

답변

3
p = re.search(r'Profile\t+(\w+)',s) 
d = re.search(r'BuildDate\t+([A-z0-9-]+)',s) 

return (p.group(1) if p is not None else None, 
     d.group(1) if d is not None else None) 

이러한 방식 : 이하 장황

return (p and p.group(1), d and d.group(1)) 

하지만, 조금 더 모호.

+0

매우 우아하고 정확하게 내가 찾고있는 것이 었습니다. 감사! – pedram

관련 문제