John Posner wrote:
On Fri, 01 Jan 2010 21:01:04 -0500, Cousin Stanley
<cousinstan...@gmail.com> wrote:
<snip>
I was not familiar with the re.finditer method
for searching strings ...
Stanley and Dave --
So far, we've just been using finditer() to perform standard-string
searches (e.g. on the word "red"). Since Dave now wants to color
multiple words the same color (e.g. the words in redList), we can use
a single regular-expression search to locate *all* the words in a
list. This eliminates the need to use a "for" loop to handle the list.
Here's what I mean:
>>> import re
>>> s = "it is neither red nor crimson, but scarlet, you see"
########## individual searches
>>> [matchobj.span() for matchobj in re.finditer("red", s)]
[(14, 17)]
>>> [matchobj.span() for matchobj in re.finditer("crimson", s)]
[(22, 29)]
>>> [matchobj.span() for matchobj in re.finditer("scarlet", s)]
[(35, 42)]
########## one "swell foop"
>>> redList = "red crimson scarlet".split()
>>> redList_regexp = "|".join(redList)
>>> redList_regexp
'red|crimson|scarlet'
>>> [matchobj.span() for matchobj in re.finditer(redList_regexp, s)]
[(14, 17), (22, 29), (35, 42)]
-John
Thanks again John,
This is fun!!!
I made a "red.text" file to hold the "red words", they are separated by
a space in the file.
Still need to add the extra parameter "color" someplace. But this is
what I have so far.
##############
file = 'red.txt'
file = open("red.txt","r")
rList = file.readlines()
file.close()
redList = str(rList).split()
blueList = "blue ball".split()
greenList = "green grass".split()
def get_complete_text(event):
complete_text = Tbox.get("1.0", END)
Tbox.tag_remove("red", "1.0", END)
Tbox.tag_remove("blue", "1.0", END)
Tbox.tag_remove("green", "1.0", END)
####RED########
redList_regexp = "|".join(redList)
for matchobj in re.finditer(redList_regexp, complete_text):
start,end = matchobj.span()
Tbox.tag_add("red", "1.0 + %d chars" % start,"1.0 + %d chars" % end)
Tbox.tag_config("red", foreground="red")
####BLUE#######
blueList_regexp = "|".join(blueList)
for matchobj in re.finditer(blueList_regexp, complete_text):
start,end = matchobj.span()
Tbox.tag_add("blue", "1.0 + %d chars" % start,"1.0 + %d chars" %
end)
Tbox.tag_config("blue", foreground="blue")
####GREEN#######
greenList_regexp = "|".join(greenList)
for matchobj in re.finditer(greenList_regexp, complete_text):
start,end = matchobj.span()
Tbox.tag_add("green", "1.0 + %d chars" % start,"1.0 + %d chars"
% end)
Tbox.tag_config("green", foreground="green")
--
http://mail.python.org/mailman/listinfo/python-list