Removing any single letter on a string in python

点点圈 提交于 2020-12-25 10:01:13

问题


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 343fsdfd'

Been trying to figure out for a while with regex without success. I know how to cut things from certain char/symbol in two pieces, but not what I am trying to do.

I have been tinkering with

re.sub(r'^[^ ]*', '', text)

回答1:


>>> ' '.join( [w for w in input.split() if len(w)>1] )
'23rwqw 34qf34 343fsdfd'



回答2:


import re
text = "z 23rwqw a 34qf34 h 343fsdfd"
print re.sub(r'(?:^| )\w(?:$| )', ' ', text).strip()

or

tmp = re.sub(r'\b\w\b', ' ', input)
print re.sub(r'\s{2,}', ' ', tmp).strip()



回答3:


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.




回答4:


I had a similar issue and came up with the following regex solution:

import re
pattern = r"((?<=^)|(?<= )).((?=$)|(?= ))"
text = "z 23rwqw a 34qf34 h 343fsdfd"
print(re.sub("\s+", " ", re.sub(pattern, '', text).strip()))
#23rwqw 34qf34 343fsdfd

Explanation

  • (?<=^) and (?<= ) are look-behinds for start of string and space, respectively. Match either of these conditions using | (or).
  • . matches any single character
  • ((?=$)|(?= )) is similar to the first bullet point, except it's a look-ahead for either the end of the string or a space.

Finally call re.sub("\s+", " ", my_string) to condense multiple spaces with a single space.




回答5:


try this one;

(?<![\w])(?:[a-zA-Z0-9](?: |$))


来源:https://stackoverflow.com/questions/32705962/removing-any-single-letter-on-a-string-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!