Mini Program 3 - Pattern Matching in a text file
Hairbrow is stuck in her task. She has to search all occurrences of a word in a huge text file. As lazy to do a manual work, she approaches her buddy Knowddy for help. Then, they code not only to search words but also to highlight all occurrences and save the newly created output file.
Try to create your own python code from the pseudo code shared below. Here's the Knowddy's and Hairbrow's version of Python code (GitHub Link)
Visuals are created using canva
Recently i was working on problem where I had to remove consecutive repetitive words
ReplyDeleteI used following logic
import re
pattern = r'\b(?P\w+)(\s(?P=word))+'
text = 'twinkle twinkle little star, the the the game has begun, please come here soon please '
repl = r'\g'
new_text = re.sub(pattern, repl, text)
print(new_text)
twinkle little star, the game has begun, please come here soon please
repl= r'\gopen tag word close tag'
Deletesome how tag is getting removed after publish, so have to write in words above
Thanks for sharing the code and information
DeleteThat's awesome Shekhar. Thanks for sharing your experience with Python development.
Delete