I want to find all occurrences of a given phrase in a passage. The phrases are user inputs and cannot be predicted beforehand.
One solution is to use regex to search (findall, finditer) the phrase in the passage:
import re
phrase = "24C"
passage = "24C with"
inds = [m.start() for m in re.finditer(phrase, passage)]
Then the result is
inds = [0]
Because the phrase matches the passage at index 0 and there is only one occurrence.
However, when the phrase contains characters that have special meanings in regex, things are trickier
import re
phrase = "24C (75F)"
passage = "24C (75F) with"
inds = [m.start() for m in re.finditer(phrase, passage)]
Then the result is
inds = []
This is because the parentheses are interpreted specially as a regex pattern, but this is not desirable as I only want to have literal matches.
Is there anyway to enforce the phrase to be treated as string literal, not a regex pattern?