全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
2796 4
2017-06-07
悬赏 5 个论坛币 未解决
请问创造一个函数input两个变量,如何在re.compile里面来表达这个变量?
#!python3
# deleteBlank.py - to delete the blank as same as strip() or delete the specific string
#create a string
import re
def deleteBlank(string,delete):
    print('deleteBlank is to delete the blank at the start or end of the string as same as what strip do or delete the specific part in the string')
    if delete=='':
        #it works as same as the string.strip()
        #find the string maybe start with the blank
        blankRegex=re.compile(r'^(\s)*(.*)(\s)*$',re.DOTALL)
        print(blankRegex.sub(r'\2',string))
    else:
        findRegex=re.compile(r'(.*)[delete](.*)',re.DOTALL)
        print(findRegex.sub(r'\1\2',string))


deleteBlank('I love beautiful girls.','beautiful')
deleteBlank('  hello world  ','')

结果是这样的
deleteBlank is to delete the blank at the start or end of the string as same as what strip do or delete the specific part in the string
I love beautiful girs.
deleteBlank is to delete the blank at the start or end of the string as same as what strip do or delete the specific part in the string
hello world         

求解答,怎么样才能够解决这个问题?


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-6-8 17:04:53
lobster_future 发表于 2017-6-7 16:08
请问创造一个函数input两个变量,如何在re.compile里面来表达这个变量?
#!python3
# deleteBlank.py - t ...
%s 可以放在字符中 在字符之后加上%(变量名)就可以在string中表达变量了
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-6-9 15:42:06
为什么自己解答出来了自己的问题 不可以给自己发布悬赏呢?捉急
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-6-9 15:42:26
是不是只要向自己申请奖励就好
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-6-9 15:53:32
使用‘%s’%(variable_name)来替代变量
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群