Python中re.error: unbalanced parenthesis at position 88

问题:re.error: unbalanced parenthesis at position 88

在使用Python正则表达式的时候,用替换方法出现上述错误,

下面是错误代码:

new_url = "https://cdn.jsdelivr.net/gh/crazyjums/crazyjums.github.io@master/images/article/20191227161526664.png)"
old_url = "https://img2018.cnblogs.com/blog/1212206/201912/1212206-20191227161526664-2133632479.png)"
line = "![list example](https://img2018.cnblogs.com/blog/1212206/201912/1212206-20191227161526664-2133632479.png)"
new_line = re.sub(old_url,new_url,line)
print(new_line)

错误提示:

error

错误原因:

reason

我们可以看到,需要替换的两个字符串末尾多出一个)来,导致匹配出错。我们去掉末尾那个)就可以了。

更正代码:

    line = '![list example](https://img2018.cnblogs.com/blog/1212206/201912/1212206-20191227161526664-2133632479.png)'
    old_url = "https://img2018.cnblogs.com/blog/1212206/201912/1212206-20191227161526664-2133632479.png"
    new_url = "https://cdn.jsdelivr.net/gh/crazyjums/crazyjums.github.io@master/images/article/20191227161526664.png"
    l = re.sub(old_url,new_url,line)
    print(line)
    print(l)

正确运行结果:

right result
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 类 !/usr/bin/python -- coding: UTF-8 -- class Employee:'所有...
    SkTj阅读 4,558评论 0 0
  • 常用模块 认识模块 什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文...
    go以恒阅读 6,096评论 0 6
  • re模块手册 本模块提供了和Perl里的正则表达式类似的功能,不关是正则表达式本身还是被搜索的字符串,都可以...
    喜欢吃栗子阅读 9,474评论 0 13
  • Python中的正则表达式(re) import rere.match #从开始位置开始匹配,如果开头没有则无re...
    BigJeffWang阅读 12,014评论 0 99
  • 那个湛蓝的六月,装饰在格子里的几行甜蜜温暖了我冷冻如冰的心,感触的洪流从此开闸,猝不及防的磅礴,不采取半点防御...
    小青虫13阅读 1,404评论 0 4