Python

Regular expression operation (re.sub)

벤진[Benzene] 2020. 8. 16. 03:36

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