내가 답을 찾을 수 있지만, 일부 온라인 경험이 있는 I believe 는 정규 표현식, 여기서 필요한 것은 없어요.
I have a & # 39 의해 분할됩니다 하는 문자열으로 인컨텍스트 & # 39;;; 또는 & # 39, & # 39;; 즉, 한 공간에 쉼표와 세미콜론이나 될 수 있다. 개별 쉼표 뒤에 공백 없이 그대로 둬야 합니다
예 구체화하십시오:
"b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3], mesitylene [000108-67-8]; polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]"
/dev/raw/raw200 분할됩니다 바뀌엇어요 redboot*용 다음과 같습니다.
('b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3]' , 'mesitylene [000108-67-8]', 'polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]')
다행히 Python 은 이 내장:)
import re
re.split('; |, ',str)
>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']
>>> import re
>>> delimiters = "a", "...", "(c)"
>>> example = "stackoverflow (c) is awesome... isn't it?"
>>> regexPattern = '|'.join(map(re.escape, delimiters))
>>> regexPattern
'a|\\.\\.\\.|\\(c\\)'
>>> re.split(regexPattern, example)
['st', 'ckoverflow ', ' is ', 'wesome', " isn't it?"]
[레오 스카피] [이스케이프입니다] 에서는 자동으로 구축할 수 있는 패턴을 이스케이프된 아주 잘 구분.
이 솔루션은 here& # 39 의 함수로 복사 붙여 넣기 단축시킵니다 더함인가:
def split(delimiters, string, maxsplit=0):
import re
regexPattern = '|'.join(map(re.escape, delimiters))
return re.split(regexPattern, string, maxsplit)
[이스케이프입니다]: http://docs.python.org/2/library/re.html # 레오 스카피 [분리-정합성]: http://docs.python.org/2/library/re.html # 레오르게소브ject.스플리트
이에 대해 s # 39 위, 이 문제는 특정 구분, Jonathan& 오토메이티드 일하고 있는 것으로 보인다. 예를 들면 다음과 같습니다.
>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']
>>> b='1999-05-03 10:37:00'
>>> re.split('- :', b)
['1999-05-03 10:37:00']
구분 기호 대괄호의 효과적으로 작업할 두고 있는 것으로 보인다.
>>> re.split('[- :]', b)
['1999', '05', '03', '10', '37', '00']