2013-02-14 2 views

답변

8

을 반환 예를 들어,이 문장

this is a wo!rd right !and| other| hello |other 

에 그래서 당신은이를 사용할 수 있습니다

>>> sentence = "this is a wo!rd right !and| other| hello |other" 

>>> import re 

>>> re.findall("\S*[^\w\s]\S*", sentence) 
['wo!rd', '!and|', 'other|', '|other'] 

이 모든 단어를 찾을 수 있습니다, 적어도 1 non-word, non-space 문자를 포함하는. \S[^\s]과 같습니다.

정규식 설명 : 대신 * \ w * [^ \ w \ S] \ w를 사용하여 문제

\S*  # Match 0 or more non-space character 
[^\w\s] # Match 1 non-space non-word character 
\S*  # Match 0 or more non-space character 
+0

무엇? – Sofia

+0

@sofia. 네. '! and |'에서'and |'만 일치합니다. '! '는'\ S'와 일치하지만'\ w'와는 일치하지 않습니다. –

+0

네, 맞아요. 감사 :) – Sofia

관련 문제