Function Repository Resource:

HighlightText

Source Notebook

Highlight parts of text according to a given pattern

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["HighlightText"]["text",patt]

highlights the substrings in "text" that match the string expression patt.

ResourceFunction["HighlightText"]["text",pattcol]

highlights the strings matching patt using the background color col.

ResourceFunction["HighlightText"]["text",{p1col1,p2col2,}]

highlights each of the pi with color coli.

ResourceFunction["HighlightText"]["text",patt,n]

highlights up to n occurrences of patt.

Details and Options

The string expression patt can contain any of the objects specified in the notes for StringExpression.
The color specified by col can be any valid color (as determined by ColorQ), such as RGBColor, Hue and GrayLevel.
In ResourceFunction["HighlightText"]["text",patt,n], the value for n can be a non-negative integer, All or Automatic.
ResourceFunction["HighlightText"] has the following options:
"TrimmingThreshold"Nonethe max number of characters to display around highlighted sections
IgnoreCaseFalsewhether to treat lowercase and uppercase letters as equivalent
"DefaultColor"the default color to use when no color is explicitly given for a pattern

Examples

Basic Examples (4) 

Highlight part of some text:

In[1]:=
ResourceFunction["HighlightText"]["This is some test text.", "test"]
Out[1]=

Use a string pattern:

In[2]:=
ResourceFunction["HighlightText"]["the cat in the hat", "a" ~~ __ ~~ "e"]
Out[2]=

Highlight dates using DatePattern:

In[3]:=
ResourceFunction[
 "HighlightText"]["On 31/12/2003 we left, and on 5/3/2004 we came back", DatePattern[{"Day", "Month", "Year"}]]
Out[3]=

Provide a list of items to highlight:

In[4]:=
ResourceFunction[
 "HighlightText"]["the cat in the hat", {_ ~~ "at", "in"}]
Out[4]=

Scope (4) 

Specify a color for highlighting:

In[5]:=
ResourceFunction[
 "HighlightText"]["item13, task15, item11, var4, item2", "item" ~~ DigitCharacter .. -> Blue]
Out[5]=

Specify a different color for each pattern:

In[6]:=
ResourceFunction[
 "HighlightText"]["abc 123 def 456", {DigitCharacter -> Blue, LetterCharacter -> Green}]
Out[6]=

Font color is automatically chosen based on the highlight color:

In[7]:=
ResourceFunction[
 "HighlightText"]["Font color is automatically chosen based on the highlight color.", {"automatically" -> Yellow, "highlight" -> Purple}]
Out[7]=

Limit the number of items to highlight:

In[8]:=
ResourceFunction[
 "HighlightText"]["buffalo from buffalo, which buffalo from buffalo bully, themselves bully buffalo from buffalo.", "buffalo", 3]
Out[8]=

Options (7) 

DefaultColor (1) 

Change the default highlight color:

In[9]:=
ResourceFunction["HighlightText"]["This is some test text.", "test", "DefaultColor" -> LightOrange]
Out[9]=

IgnoreCase (2) 

By default, pattern matching is case-sensitive:

In[10]:=
ResourceFunction[
 "HighlightText"]["Buffalo from Buffalo, which buffalo from Buffalo bully, themselves bully buffalo from Buffalo.", "Buffalo"]
Out[10]=

Ignore case instead by setting IgnoreCaseTrue:

In[11]:=
ResourceFunction[
 "HighlightText"]["Buffalo from Buffalo, which buffalo from Buffalo bully, themselves bully buffalo from Buffalo.", "Buffalo", IgnoreCase -> True]
Out[11]=

TrimmingThreshold (4) 

Only show text in the vicinity of highlighted areas:

In[12]:=
ResourceFunction["HighlightText"][
 ResourceData["Gettysburg Address"], "testing", "TrimmingThreshold" -> 10]
Out[12]=

Show more text:

In[13]:=
ResourceFunction["HighlightText"][
 ResourceData["Gettysburg Address"], "testing", "TrimmingThreshold" -> 50]
Out[13]=

Don’t trim any text:

In[14]:=
ResourceFunction["HighlightText"][
 ResourceData["Gettysburg Address"], "testing", "TrimmingThreshold" -> None]
Out[14]=

If the original text does not have line breaks, the trimmed parts will appear inline:

In[15]:=
ResourceFunction[
 "HighlightText"]["Buffalo from Buffalo, which buffalo from Buffalo bully, themselves bully buffalo from Buffalo.", "bully", "TrimmingThreshold" -> 5]
Out[15]=

If the original text has line breaks, the trimmed parts will each appear on their own line:

In[16]:=
ResourceFunction["HighlightText"]["Buffalo from Buffalo,
which buffalo from Buffalo bully,
themselves bully buffalo from Buffalo.", "bully", "TrimmingThreshold" -> 5]
Out[16]=

Applications (1) 

Search for a word in a large body of text:

In[17]:=
ResourceFunction["HighlightText"][
 ResourceData["War and Peace"], "Hurrah-ah-ah!", "TrimmingThreshold" -> 200]
Out[17]=

Possible Issues (2) 

The highlighting wraps tightly around text, so heights may not match:

In[18]:=
ResourceFunction[
 "HighlightText"]["aaaAAAaaaAAA", {"a" -> Blue, "A" -> Black}]
Out[18]=

Consecutive matches are grouped together and will have the same highlighting height:

In[19]:=
ResourceFunction["HighlightText"]["AAA aaaAAAaaaAAA aaa", "a" | "A" -> Black]
Out[19]=

Neat Examples (2) 

Highlight the different writing scripts being used in a Japanese sentence:

In[20]:=
ResourceFunction[
 "HighlightText"]["このテキストは日本語です。", {_?ResourceFunction["HiraganaQ"] ->
    LightRed, _?ResourceFunction["KatakanaQ"] -> LightGreen, _?ResourceFunction["KanjiQ"] -> LightBlue}]
Out[20]=

Interactively highlight words based on the number of characters:

In[21]:=
Manipulate[
 ResourceFunction["HighlightText"][
  "Just over twenty years ago I made what at first seemed like a small discovery: a computer experiment of mine showed something I did not expect. But the more I investigated, the more I realized that what I had seen was the beginning of a crack in the very foundations of existing science, and a first clue towards a whole new kind of science. This book is the culmination of nearly twenty years of work that I have done to develop that new kind of science. I had never expected it would take anything like as long, but I have discovered vastly more than I ever thought possible, and in fact what I have done now touches almost every existing area of science, and quite a bit besides.", RegularExpression["\\b\\w{" <> ToString[u] <> "}\\b"]], {u, 1, 10, 1}]
Out[21]=

Version History

  • 1.0.0 – 10 July 2019

Related Resources

License Information