1
$\begingroup$

given a String:

string = "the cat is on the mat"

and given a String-Pattern

stringPattern = "c" ~~ _ ~~ "t"

question

write a function that formats its output such that every occurrence of the String-Pattern in the String is Highlighted

 f[string, stringPattern]

enter image description here

$\endgroup$
1
  • 4
    $\begingroup$ Not to discourage answers here, just as a reference, there is this ResourceFunction["HighlightText"]["This is some test text.", "test"] $\endgroup$ – Jason B. May 27 at 11:10
3
$\begingroup$
f = StringReplace[#, pat : #2 :> ToString[Highlighted@pat, StandardForm]] &;

f[string, stringPattern]

enter image description here

f[string <> " " <> string, stringPattern]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.