Find the indexes of all regex matches?

前端 未结 3 1992
不思量自难忘°
不思量自难忘° 2020-11-28 05:00

I\'m parsing strings that could have any number of quoted strings inside them (I\'m parsing code, and trying to avoid PLY). I want to find out if a substring is quoted, and

3条回答
  •  忘掉有多难
    2020-11-28 05:05

    #To get indice of all occurence

    S = input() # Source String 
    k = input() # String to be searched
    import re
    pattern = re.compile(k)
    r = pattern.search(S)
    if not r: print("(-1, -1)")
    while r:
        print("({0}, {1})".format(r.start(), r.end() - 1))
        r = pattern.search(S,r.start() + 1)
    

提交回复
热议问题