找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 15418|回复: 3

[问题求助] Python删除问号开始后面的全部字符错在哪里?

[复制链接]
发表于 2020-3-11 22:24:06 | 显示全部楼层 |阅读模式
  1. #!/usr/bin/python

  2. import re
  3. str = "this is string ?example....wow!!! this is really string";
  4. print re.sub(r'^?.','',str);
复制代码
我想删除?开始后面的全部字符,用正则的方法,
报错无法运行。
发表于 2020-3-12 10:20:45 | 显示全部楼层
回复 1# netdzb
  1. import re
  2. str = "this is string ? example....wow!!! this is really string"
  3. print re.sub(r'(?<=[?|?]).+','',str)
复制代码
测试中发现,你的问号是中文的,顺手做了一个中文匹配,不过有点bug
真心不喜欢你的代码风格。。。
 楼主| 发表于 2020-3-20 18:58:59 | 显示全部楼层
回复 2# wujunkai

我刚才发的新问题,好像就是要做个中文字符的判断。该如何修改?
谢谢!
发表于 2020-3-20 20:29:45 | 显示全部楼层
回复 3# netdzb
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-

  3. import re
  4. str = "this is string ?example....wow!!! this is really string"
  5. str = re.sub(r'(?<=[?|?]).+','',str)
  6. if str[-1]!='?' :
  7.     str=str[:-1]+'?'
  8. print str
复制代码
不要总想着一行搞定,该多写点还是要写的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:24 , Processed in 0.018022 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表