, 나는 정규 표현식을 만든 : 권장파이썬과 루블러 사이의 정규 표현식의 차이점은 무엇입니까? Rubular 나에서
파이썬에서 정규 표현식의 사용이다?: 컴퓨터와 예술의 일부 좋은 안락 수준
(Prerequisite|Recommended): (\w|-|)*
그것은 굵게 일치합니다.
여름. 2 학점. 선행 조건 : 신입생 서 또는 강사의 허락을 받으십시오. 공학 학위를 받기 위해 신용이 적용되지 않을 수 있습니다. . S-U 성적 만.
note_re = re.compile(r'(Prerequisite|Recommended): (\w|-|)*', re.IGNORECASE) def prereqs_of_note(note): match = note_re.match(note) if not match: return None return match.group(0)
불행하게도, 코드 대신 경기
None
을 반환합니다 :>>> import prereqs >>> result = prereqs.prereqs_of_note("Summer. 2 credits. Prerequisite: pre-fres hman standing or permission of instructor. Credit may not be applied toward engi neering degree. S-U grades only.") >>> print result None
내가 잘못 여기서 뭐하는 거지 여기
업데이트 :re.match()
대신 re.search()
이 필요합니까?
http://pythex.org/는 파이썬의 엔진을 사용해도 정규 표현식이 해당 문자열과 일치하므로 정규식 (파이썬을 모른다)을 어떻게 사용하고 있는지 문제가 있다는 것을 알고 있습니다. – Gareth
또한 개인적으로는 regex를'(Prerequisite | Recommended) : ([\ w -] *)'로 업데이트하면 나머지 일치를보다 잘 포착 할 수 있습니다. (http://rubular.com/r/5v7u66vc1M 참조) – Gareth