python - 删除python字符串中数字1之前的数字
问题描述
我想知道是否可以在字符串中的数字 1 之前删除数字:
original_string = 'Word 2 def 1 here 2 what 3 for 4 that'
desired_string = 'Word def 1 here 2 what 3 for 4 that'
解决方案
您也可以使用它:
import re
original_string = 'Word 2 def 4 1 here 2 what 3 for 4 that'
if '1' in original_string:
indexfirst=int(original_string.index('1'))
else:
indexfirst=0
original_string=re.sub('[023456789]', '', original_string[0:indexfirst])+original_string[indexfirst:]
print(original_string)
推荐阅读
- xml - 如何将 xmlns 命名空间添加到站点地图 xml 中的 urlset
- javascript - javascript返回嵌套对象数组并排列相似类型
- html - 使用css显示两个相邻的div,中间有空格
- javascript - 如何设置发送与 Google 表单上的调查问卷相关联的自动提醒电子邮件?
- android - 模糊 TextView 中的特定单词
- javascript - jquery 数据表,添加额外的分页号以通过 ajax 加载下一组数据
- flutter - google_maps_flutter 和 AndroidX 迁移
- html - 如何在列中具有相等大小的图像的等高列
- bash - Git - 如果没有任何匹配项,我如何将配置添加到“选项卡”以获取正确的分支或分支?
- java - 使用 javafx 播放音效/音乐的最佳选择是什么?