본문 바로가기
Python

Regular expression operation (re.sub)

by 벤진[Benzene] 2020. 8. 16.

re.sub(pattern, repl, string, count=0, flags=0)

s = re.sub('[^a-z0-9]','',s)
#[a-z0-9] --> matching any lower case letter a to z and digit of 0-9
'''
This will replace any special characters in 's' string with non-space blank characters.
'''

-Example-

s = 'abc#@%921'

s = re.sub('[^a-z0-9]', '', s)

 

s --> 'abc921'

 

https://docs.python.org/3/library/re.html#re.sub

 

re — Regular expression operations — Python 3.8.5 documentation

This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings (str) as well as 8-bit strings (bytes). However, Unicode strings and 8-bit strings cannot be mixed:

docs.python.org

 

'Python' 카테고리의 다른 글

f-string  (0) 2020.08.12
Enumerate  (0) 2020.08.12
Type Hint  (0) 2020.08.11

댓글