我想在洗掉非字母字符后編輯一個字串,然后再次將它們移動到正確的位置。例如:
import re
string = input()
alphastring = re.sub('[^0-9a-zA-Z] ', '', string)
alphastring = alphastring[::2]
我希望它如下:
- string = "heompuem ykojua'rje awzeklvl."
- alphastring = heompuemykojuarjeawzeklvl
- alphastring =希望你好
- ??????= 希望你一切都好。
我嘗試用不同的解決方案解決問題,但沒有一個給我正確的輸出。我求助于使用我不太熟悉的 RegEx。任何幫助將受到極大歡迎。
uj5u.com熱心網友回復:
我想不出任何合理的方法來使用正則運算式來做到這一點。只需使用從輸入復制到輸出的回圈,跳過所有其他字母數字字符。
copy_flag = True
string = input()
alphastring = ''
for c in string:
if c.isalnum():
if copy_flag:
alphastring = c
copy_flag = not copy_flag
else:
alphastring = c
uj5u.com熱心網友回復:
這是一種方法:
string = "heompuem ykojua'rje awzeklvl."
result, i = "", 0
for c, alph in ((c, c.isalnum()) for c in string)
if not (alph and i%2): # skip odd alpha-numericals
result = c
i = alph # only increment counter for alpha-numericals
result
# "hope you're well."
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333193.html
