2015-01-16 3 views

답변

1

예 1 : 은 (대부분의 쉬운 방법은 내가 생각하는)

# String 
string = 'ABC#[email protected]' 
# Remove the #, @ and 7 from the string. (You can choose whatever you want to take out.) 
line = re.sub('[#@7]', '', string) 
# Print output 
print(line) 

예 2 :

또한, 문자열을 사용하면 모든 위치에서 볼 수있는 방법을 만들 수있는 편지와 일치하는 경우 네가 끈에 넣고 싶지 않은 편지. 편지를 지우고 싶으면 그 편지를 새 문자열에 추가하십시오. 그렇지 않으면 패스합니다.

예 : 문자열 's'은 문자열이고 'd'는 필터링하려는 문자입니다.

s = 'abc/geG#[email protected]' 
d = '#@/' 
e = '' 
i = 0 
j = 0 
l = len(s) 
g = len(d) 
a = False 

while i < l: 
    # Grab a single letter 
    letter1 = s[i:i+1] 
    # Add +1 
    i = i + 1 
    # Look if the letter is in string d 
    while j < g: 
     # Grab a single letter 
     letter2 = d[j:j+1] 
     # Add +1 
     j = j + 1 
     # Look if letter is not the same. 
     if (letter1 != letter2): 
      a = True 
     else: 
      a = False 
      break 
    # Reset j 
    j = 0 
    # Look if a is True of False. 
    # True: Letter does not contain letters from string d. 
    # False: Letter does contain letters from string d. 
    if (a == True): 
     e = e + letter1 
    else: 
     pass 

# Print the result 
print(e) 
1

문자 클래스 내에서 백 슬래시를 이스케이프 처리해야합니다. 일반 문자열에 원시 문자열을 사용하면 더 이상 이스케이프 할 필요가 없습니다.

cadena = re.sub(r'[^0-9a-zA-Z\\/@+\-:,|#]+', '', cadena) 

밑줄 문자는 영숫자로 간주됩니다. 스트립을 제거 할 필요가 없으면 표현을 단순화 할 수 있습니다.

cadena = re.sub(r'[^\w\\/@+\-:,|#]+', '', cadena) 
관련 문제