2016-11-17 1 views
1

오픈 소스 라이센스 유형을 대부분의 파일 시작 부분에있는 주석 아웃 코드와 일치 시키려고합니다. 그러나 원하는 문자열 (예 : 약소 일반 공중 사용 허가서)이 두 줄에 걸쳐있는 상황에서 어려움을 겪고 있습니다. 라이센스 예를 들어 아래의 코드를 참조하십시오. 정규식의 전환 확인을 사용하여Python Regex in comments 코드

* Copyright (c) Codice Foundation 
* <p/> 
* This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser 
* General Public License as published by the Free Software Foundation, either version 3 of the 
* License, or any later version. 
* <p/> 
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* Lesser General Public License for more details. A copy of the GNU Lesser General Public License 
* is distributed along with this program and can be found at 
* <http://www.gnu.org/licenses/lgpl.html>. 
*/ 

인해 주석 코드에서 공간의 알 수없는 숫자뿐만 아니라 다른 언어로 다른 주석 문자 수 없습니다. 현재 정규 표현식의 예는 다음과 같습니다.

self._cr_license_re['GNU']       = re.compile('\sGNU\D') 
self._cr_license_re['MIT License']     = re.compile('MIT License|Licensed MIT|\sMIT\D') 
self._cr_license_re['OpenSceneGraph Public License'] = re.compile('OpenSceneGraph Public License', re.IGNORECASE) 
self._cr_license_re['Artistic License']    = re.compile('Artistic License', re.IGNORECASE) 
self._cr_license_re['LGPL']       = re.compile('\sLGPL\s|Lesser General Public License', re.IGNORECASE) 
self._cr_license_re['BSD']       = re.compile('\sBSD\D') 
self._cr_license_re['Unspecified OS']     = re.compile('free of charge', re.IGNORECASE) 
self._cr_license_re['GPL']       = re.compile('\sGPL\D|(?<!Lesser)\sGeneral Public License', re.IGNORECASE) 
self._cr_license_re['Apache License']     = re.compile('Apache License', re.IGNORECASE) 
self._cr_license_re['Creative Commons']    = re.compile('\sCC\D') 

정규식을 사용하여이 문제를 해결하는 방법에 대한 제안을 환영합니다.

+0

"줄을 하나의 긴 문자열로 묶는 방법 만 있다면"? – usr2564301

+0

무엇이 문제입니까? ''OpenSceneGraph Public License ''(및 어디서든)의 모든 리터럴 공백을 모두'\ s +'로 바꾸십시오. –

답변

1

그런 다음 거기에 라이센스를 찾을 수 있습니다, 한 줄에 여러 줄의 코멘트를 넣어해야 this regex을 사용하고 공간

\s*\*\s*\/? 

이로 교체 할 수있다.

+0

좋습니다. 그러나 위의 정규 표현식은 개행 문자 ('\ n')를 제거하지 않았습니다. 결과는 다음과 같습니다 : 'text = fid.read(). replace ('\ n', '') fin_text = re.sub ('\ * \ s * \ /?', text) ' – lmum27