I would like to remove any single letter from a string in python.
For example:
input: \'z 23rwqw a 34qf34 h 343fsdfd\'
output: \'23rwqw 34qf34 343fsd
-
I hope there's a neater regex way than this, but:
>>> import re
>>> text = 'z 23rwqw a 34qf34 h 343fsdfd'
>>> re.sub('(\\b[A-Za-z] \\b|\\b [A-Za-z]\\b)', '', text)
'23rwqw 34qf34 343fsdfd'
It's a word boundary, a single letter, a space, and a word boundary.
It's doubled up so it can match a single character at the start or end of the string z_
and _z
leaving no space, and a character in the middle _z_
leaving one space.
- 热议问题