场景:找到字符串结尾的所有‘-’,str = ‘45555----’, pattern = ‘-{1,}$’
不同语言比较:
使用re在线测试工具正则表达式在线测试 | 菜鸟工具 (runoob.com) 结果如下:
在JavaScript,PHP,Go,JAVA,Ruby中都是直接用pattern去匹配str,匹配范围是整个字符串,而python的re.match()是从字符串起始位置进行匹配,所以要用re.compile得到一个pattern再用pattern.search(string[, pos[, endpos]]) 或re.search(pattern, string[, flags])进行搜索(pos和endpos的默认值分别为0和len(string))。
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a Match object, or None if no match was found.
match = re.match('-$', '786-', 0) # return None