Ritu Singh
Problem:
Suppose I have a list of keywords and an input string labelled section, I want my code to find those keywords within section and put them inside [] square brackets. However, my keywords sometimes overlap with each other.
To fix that I sorted them by length so that the longer keywords would be prioritized. However, when I run my code, sometimes I would get double or nested brackets (I assume it is because it still detects those as valid keywords)
I tried this:
Do not mind the line splits, it is for another part of to handle multiple lines.
I wanted: [alpha] [alpha beta] [alpha beta charlie] [alpha beta charlie delta]
but instead I got: [alpha] [alpha] beta] [alpha] beta] charlie] [alpha] beta] charlie] delta]
Solution:
You can join the sorted keywords into an alternation pattern instead of substituting the string with different keywords 4 times, each time potentially substituting the replacement string from the previous iteration:
This outpouts:
Suggested blogs:
>Can I make a specific character the center point on a header?
>Authenticating user using PassportJS local strategy during registration
>How to get pending ActiveMQ messages?
>How to pull out different elements from a text file? - Python
>Adding cooldown for user tries to farm XP-discord.py