楼主: lobster_future
2840 4

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

  • 1关注
  • 0粉丝

已卖:1份资源

本科生

84%

还不是VIP/贵宾

-

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

楼主
lobster_future 发表于 2017-6-7 16:08:44 |AI写论文
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中表达变量了

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

板凳
lobster_future 发表于 2017-6-9 15:42:26
是不是只要向自己申请奖励就好

报纸
lobster_future 发表于 2017-6-9 15:53:32
使用‘%s’%(variable_name)来替代变量

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-5 11:32