2
$\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
  • 5
    $\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, 2021 at 11:10

1 Answer 1

4
$\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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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