Find the indexes of all regex matches?

前端 未结 3 1959
不思量自难忘°
不思量自难忘° 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:03

    This is what you want: (source)

    re.finditer(pattern, string[, flags]) 
    

    Return an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result unless they touch the beginning of another match.

    You can then get the start and end positions from the MatchObjects.

    e.g.

    [(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-28 05:26

    This should solve your issue pattern=r"(?=(\"[^\"]+\"|'[^']+'))"

    Then use the following to get all overlapping indices,

    indicesTuple=[(mObj.start(1),mObj.end(1)-1) for mObj in re.finditer(pattern,input)]

    0 讨论(0)
提交回复
热议问题