# findall2 - Finds all regex matches in a string, for each match return
# the matched string and the start character of the match.
# (Returns a list of tuples: (match,start))
# (Python's findall extended to returns start position also)
#=============================================================================
import re, sys
def findall2(pattern,string):
regex = re.compile(pattern,re.IGNORECASE | re.DOTALL)
pos = 0
matched = regex.search(text,pos)
matches = []
while matched:
groups = matched.groups()
value = groups[0]
start = matched.start()
pos = matched.end() + 1
tuple = (value,start)
matches.append(tuple)
matched = regex.search(text,pos)
return matches
#----------------------------------------------------------------------
# main program:
if __name__ == "__main__":
if len(sys.argv) != 3:
text = "Once upon a time, far off in a land of fairies and goblins there lived a very cruel goblin, this goblin had a particularly ugly wart on the end of his nose..."
# 1111111111111111111111111111111111111111111111111111111111111
# 1111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
pattern = "(goblin)"
matches = findall2(pattern,text)
print matches
#print "Usage: ", sys.argv[0], "pattern string"
else:
pattern = sys.argv[1]
string = sys.argv[2]
matches = findall2(pattern,string)
print matches
Text file Source (historic): geocities.com/soho/square/3472
geocities.com/soho/squaregeocities.com/soho
(to report bad content: archivehelp @ gmail)
|
|
|
|
|