re学习心得

学习了Python的正则re后,会对其他一些知识理解更加深入,更能触类旁通:

Pythonstrstartswith

举例:

  1. imgUrl = "Books/55434/Books_55434_205865_Book_20190407102211.jpg"
  2. isImgUrlValid = imgUrl.startswith("http")
  3. # isImgUrlValid=False
  4. imgUrl = "http://img.xiaohuasheng.cn/Douban/Book/s11215709.jpg"
  5. isImgUrlValid = imgUrl.startswith("http")
  6. # isImgUrlValid=True

其中的startswith

  1. isImgUrlValid = originCoverImgUrl.startswith("http")

等价于,可以换为:

  1. isMatchHttp = re.match("^http", originCoverImgUrl)
  2. isOriginCoverImgUrlValid = isMatchHttp

而之前用startswith还有个缺点:

万一想要同时判断一个url是否是http或https开头的,却要写两个startswith的判断

再去逻辑或才能得到要的结果:

  1. imgUrl = "http://img.xiaohuasheng.cn/Douban/Book/s11215709.jpg"
  2. # imgUrl = "https://img.xiaohuasheng.cn/Douban/Book/s11215709.jpg"isHttpUrl = imgUrl.startswith("http")
  3. isHttpsUrl = imgUrl.startswith("https")
  4. isValidUrl = isHttpUrl or isHttpsUrl

而改为正则去匹配:

  1. imgUrl = "http://img.xiaohuasheng.cn/Douban/Book/s11215709.jpg"
  2. # imgUrl = "https://img.xiaohuasheng.cn/Douban/Book/s11215709.jpg"isHttpOrHttpsUrl = re.match("^https?", imgUrl)
  3. isValidUrl = isHttpOrHttpsUrl

就显得更加简洁和高效。

如此举一反三,触类旁通,可以把正则在字符串的搜索和替换等领域发挥出更大的价值。