I want to use regex in Python to find the string(s) which is empty or only contain space.
" " - True
" " - True
"" - True
" 1 " - False
How to do it via regex?
You can use re.findall
and anchor the search from the start of the string (^
) and end of the string ($
):
import re
strings = [' ', ' ', '', ' 1 ']
results = [i for i in strings if re.findall('^\s*$', i)]
Output:
[' ', ' ', '']