Hi everyone I am writing this program that takes a string and search it for words. Those words are taken and they get "<" at the beginning and ">" at the end of the word. then it prints the string with the words and the "<, >" characters.

Here is my code:

``````def test():
import re
a = "<"
b = ">"
c = "Hello this is a test"
d = re.findall(r"\bthis\b|\ba\b", c)
e = re.split(r"\bthis\b|\ba\b", c)
for item in d:
y = item
f = a+y+b
output = f.join(re.split(r"\bthis\b|\ba\b", c))
print output``````

When I run this code I get this answer:

``Hello <a> is <a> test``

However the answer I want is this:

``Hello <this> is <a> test``

Can anyone please tell me how to solve this problem? thanks in advance.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Zetlin

re.split breaks the string in the specified pattern, removing the parts which caused the split. There is a much simpler solution to this problem though. Just iterate the string by splitting it at each space and compare each word to a list of words you're watching for. You don't need regular expressions for this problem:

``````def myFunc(s):
# break the string into a list (at each space)
words = s.split(' ')
# put the words we are watching for into a list
watched = ['this', 'a']

# loop our words and check each one
# and add the result to our result list
result = []
for w in words:
# if the word is in the watched list
# add the carets to each side
if w in watched:
w = '<' + w + '>'
# add the word to the result list
result.append(w)

result = ' '.join(result)
return result``````

My result:

``````>>> s = 'Hello this is a test'
>>> myFunc(s)
'Hello <this> is <a> test'``````

A much simpler and cleaner solution, I think :P

Yes your solution is a lot simpler, thanks