Highlighting repeated matches with StringCases
String patterns will always match the longest string possible, since pre___
and post___
match anything the second pattern will swallow the whole string. It will not look for further matches inside already matched parts of the string. This is related to the Overlaps
option, but there is no way to get what we want with Overlaps
and this pattern.
With Shortest
we can modify this behavior, perhaps not to achieve what we want though:
StringCases[
txt,
Shortest[pre___ ~~ t : "the" ~~ post___ ..] :> Row[{pre, Style[t, Red], post}]
]
The best way to achieve highlighting is using StringReplace
with StyleBox
:
StringReplace[
txt,
"the" -> "\*StyleBox[\"the\", FontColor->RGBColor[1, 0, 0]]"
]
Example highlighting several different words:
StringReplace[
txt,
(t : "the" | "jumps") :> "\*StyleBox[\"" <> t <> "\", FontColor->RGBColor[1, 0, 0]]"
]
This is just like C.E.'s answer, using StringReplace
, but with a different highlighting method
highlightText[words_List] := ReplaceAll[
StringReplace[x : (Alternatives @@ words) :> Highlighted[x]]@#,
StringExpression[a__] :> Row[{a}]] &
This will work on strings with and without the keywords but, unlike C.E.'s answer, when the keywords are present your result is not a String
, but a Row
. For display in a notebook, the difference is minimal, but it is worth taking note of.
highlightText[{"the", "jumps"}]@"the quick brown fox jumps over the lazy dog"
highlightText[{"the", "jumps"}]@"That lazy dog went to sleep"