-1

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?

MTANG
  • 508
  • 5
  • 16

1 Answers1

2

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:

['    ', ' ', '']
Ajax1234
  • 69,937
  • 8
  • 61
  • 102