楼主: lobster_future
2583 4

[问答] 请问创造一个函数input两个变量,如何在re.compile里面来表达这个变量? [推广有奖]

  • 1关注
  • 0粉丝

本科生

84%

还不是VIP/贵宾

-

威望
0
论坛币
96 个
通用积分
0
学术水平
0 点
热心指数
1 点
信用等级
5 点
经验
803 点
帖子
131
精华
0
在线时间
24 小时
注册时间
2017-1-15
最后登录
2018-4-13

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         

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


关键词:COMPILE Input comp OMP MPI 如何
沙发
lobster_future 发表于 2017-6-8 17:04:53 来自手机 |只看作者 |坛友微信交流群
lobster_future 发表于 2017-6-7 16:08
请问创造一个函数input两个变量,如何在re.compile里面来表达这个变量?
#!python3
# deleteBlank.py - t ...
%s 可以放在字符中 在字符之后加上%(变量名)就可以在string中表达变量了

使用道具

为什么自己解答出来了自己的问题 不可以给自己发布悬赏呢?捉急

使用道具

是不是只要向自己申请奖励就好

使用道具

使用‘%s’%(variable_name)来替代变量

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-6 01:32