2

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

Conor
  • 7,449
  • 1
  • 22
  • 46
  • 5
    Not to discourage answers here, just as a reference, there is this ResourceFunction["HighlightText"]["This is some test text.", "test"] – Jason B. May 27 '21 at 11:10

1 Answers1

4
f = StringReplace[#, pat : #2 :> ToString[Highlighted@pat, StandardForm]] &;

f[string, stringPattern]

enter image description here

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

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896