[ Python ] Regex 유용한 팁들!

2019. 7. 20. 00:16분석 Python/구현 및 자료

728x90

## [^aeiou] aeiou를 제외하고 한개

## [0-9] 중에 한개만 있을 때 

## [0-9]+ 중에 한개이상

## (\D\d)+ 이것이 반복적으로 있을 때 ( ) 사용 

 

## ^iteration_1(.*?).png$  

시작은 interation_1 로 시작하고 끝은 .png 이미 

re.search(^iteration_1([^"]+).png$ , text)


## iteration_2 로 시작하고 .png로 끝나면서 앞에 숫자가 0-5이면서 뒤에는 아무 숫자가 오게

re.search( ^iteration_2[0-5][0-9]+.png$ , text)

iteration_1 로 시작하고 .png로 끝나면서 중간에 숫자가 4개

re.search('^iteration_1\d{4}.png$'  , text)

영어 한 글자, 숫자 한 글자, 떨어져있는거 제거! 특수기호 제거!

그리고 양 끝 공백 제거 그리고 중간에 공백 2개있는 것도 1개로 바꾸기 

def remove(x) :
    m = re.sub(r'(?:\b[0-9a-zA-Zㄱ-ㅎㅏ-ㅣ]\b|[?!\W]+)\s*', ' ', x).strip() 
    m = " ".join(m.split())
    return m
    
    
remove("a ba aB 가나라다라 A AB 1 2 34 2313??!! 나 ㄷ ㄹ ㅁ 바 3번")

## 'ba aB 가나라다라 AB 34 2313 나 바 3번'

 

유용! 아래 블로그에서 가져온 것인데, 들어가서 다른것도 봐주시면 될 것 같다.

https://blog.naver.com/PostView.nhn?blogId=wideeyed&logNo=221347960543

def clean_text(text):
    pattern = '([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)' 
    text = re.sub(pattern=pattern, repl='', string=text)
    print("E-mail제거 : " , text , "\n")
    pattern = '(http|ftp|https)://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
    text = re.sub(pattern=pattern, repl='', string=text)
    print("URL 제거 : ", text , "\n")
    pattern = '([ㄱ-ㅎㅏ-ㅣ]+)'  
    text = re.sub(pattern=pattern, repl='', string=text)
    print("한글 자음 모음 제거 : ", text , "\n")
    pattern = '<[^>]*>'        
    text = re.sub(pattern=pattern, repl='', string=text)
    print("태그 제거 : " , text , "\n")
    pattern = r'\([^)]*\)'
    text = re.sub(pattern=pattern, repl='', string=text)
    print("괄호와 괄호안 글자 제거 :  " , text , "\n")
    pattern = '[^\w\s]'   
    text = re.sub(pattern=pattern, repl='', string=text)
    print("특수기호 제거 : ", text , "\n" )
    text = text.strip()
    print("양 끝 공백 제거 : ", text , "\n" )
    text = " ".join(text.split())
    print("중간에 공백은 1개만 : ", text )
    return text   
    
text = '(abc_def) 좋은글! (이것도 지워조) http://1234.com 감사합니다. aaa@goggle.comㅋㅋ<H1>thank you</H1>'
clean_text(text)

## result 

E-mail제거 :  (abc_def) 좋은글! (이것도 지워조) http://1234.com 감사합니다. ㅋㅋ<H1>thank you</H1> 

URL 제거 :  (abc_def) 좋은글! (이것도 지워조)  감사합니다. ㅋㅋ<H1>thank you</H1> 

한글 자음 모음 제거 :  (abc_def) 좋은글! (이것도 지워조)  감사합니다. <H1>thank you</H1> 

태그 제거 :  (abc_def) 좋은글! (이것도 지워조)  감사합니다. thank you 

괄호와 괄호안 글자 제거 :    좋은글!   감사합니다. thank you 

특수기호 제거 :   좋은글   감사합니다 thank you 

양 끝 공백 제거 :  좋은글   감사합니다 thank you 

중간에 공백은 1개만 :  좋은글 감사합니다 thank you

 

'좋은글 감사합니다 thank you'

1. Email 제거

2. 태그제거

3. 한글 자음, 모음 제거

4. 괄호와 괄호 글자 제거 (추가)

5. 특수기호제거

 

중간에 없애는 것을 잘 몰랐는데, 1,2 ,4번을 잘 활용하면 전체를 통으로 없앨 수 있을 것 같다. 

 

 

한번씩 보기!

https://hamait.tistory.com/342
https://hamait.tistory.com/342

https://www.tutorialspoint.com/python/python_reg_expressions

 

Python Regular Expressions

Python - Regular Expressions Advertisements A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX wo

www.tutorialspoint.com

https://hamait.tistory.com/342

 

정규표현식 (Regex) 정리

정규표현식은 아주 가끔가다가 쓰기때문에 항상 다시 볼때마다 헥깔리곤 하는데.. 주요 사용예를 정리하여 보았다. 나중에 찾아보기 편하라고 ;; 정규 표현식의 용어들 정규 표현식에서 사용되는 기호를 Meta문자..

hamait.tistory.com

https://blog.naver.com/PostView.nhn?blogId=wideeyed&logNo=221347960543

 

[Python] 문자열 Cleansing(클렌징)

파이썬에서 문자열을 다루다보면 요구사항에 따라 E-mail 주소 또는 URL 또는 HTML을 제거하거나 ...

blog.naver.com

 

https://stackoverflow.com/questions/57134579/how-to-remove-one-string-and-one-digit-in-python/57134625?noredirect=1#comment100797196_57134625

 

How to remove one string and one digit in python?

I want to remove one character and one digit. so it is my example text = "a ba aB 가나라다라 A AB 1 2 34 2313??!!" def remove(x) : m = re.sub( r"([\W\s\n\r\tㄱ-ㅎㅏ-ㅣ]+[0-9]{1})" ," " , x).strip()...

stackoverflow.com

 

728x90