An Elementary Introduction to the Wolfram Language
42String Patterns and Templates
In[1]:=
Click for copyable input
Out[1]=
This picks out three characters after each +:
In[2]:=
Click for copyable input
Out[2]=
Use the name x for the character after each +, and return that character framed:
In[3]:=
Click for copyable input
Out[3]=
In a string pattern, _ stands for any single character. __ (double blank) stands for any sequence of one or more characters, and ___ (triple blank) stands for any sequence of zero or more characters. __ and ___ will normally grab as much of the string as they can.
Pick out the sequence of characters between [ and ]:
In[4]:=
Click for copyable input
Out[4]=
__ normally matches as long a sequence of characters as it can:
In[5]:=
Click for copyable input
Out[5]=
Shortest forces the shortest match:
In[6]:=
Click for copyable input
Out[6]=
StringCases picks out cases of a particular pattern in a string. StringReplace makes replacements.
In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=
Use NestList to apply a string replacement repeatedly:
In[9]:=
Click for copyable input
Out[9]=
StringMatchQ tests whether a string matches a pattern.
Select common words that match the pattern of beginning with a and ending with b:
In[10]:=
Click for copyable input
Out[10]=
You can use | and .. in string patterns just like in ordinary patterns.
Pick out any sequence of A or B repeated:
In[11]:=
Click for copyable input
Out[11]=
In a string pattern, LetterCharacter stands for any letter character, DigitCharacter for any digit character and Whitespace for any sequence of white characters such as spaces.
Pick out sequences of digit characters:
In[12]:=
Click for copyable input
Out[12]=
Pick out sequences of digit characters flanked by whitespace:
In[13]:=
Click for copyable input
Out[13]=
Split a string into a list of pieces, by default breaking at spaces:
In[14]:=
Click for copyable input
Out[14]=
This uses a string pattern to decide where to split:
In[15]:=
Click for copyable input
Out[15]=
Split at newlines:
In[16]:=
Click for copyable input
Out[16]=
StringJoin joins any list of strings together. In practice, though, one often wants to insert something between the strings before joining them. StringRiffle does this.
Join strings, riffling the string "---" in between them:
In[17]:=
Click for copyable input
Out[17]=
TextString turns numbers and other Wolfram Language expressions into strings:
In[18]:=
Click for copyable input
Out[18]=
In a string template each `` is a slot for a successive argument:
In[19]:=
Click for copyable input
Out[19]=
In[20]:=
Click for copyable input
Out[20]=
You can insert any expression within a string template by enclosing it with <*...*>. The value of the expression is computed when the template is applied.
Evaluate the <*...*> when the template is applied; no arguments are needed:
In[21]:=
Click for copyable input
Out[21]=
In[22]:=
Click for copyable input
Out[22]=
In[23]:=
Click for copyable input
Out[23]=
42.1Replace each space in "1 2 3 4" with "---"»
Expected output:
Out[]=
42.2Get a sorted list of all sequences of 4 digits (representing possible dates) in the Wikipedia article on computers. »
Sample expected output:
Out[]=
42.3Extract headings in the Wikipedia article about computers, as indicated by strings starting and ending with "==="»
Sample expected output:
Out[]=
42.4Use a string template to make a grid of results of the form i+j=... for i and j up to 9. »
Expected output:
Out[]=
42.5Find names of integers below 50 that have an i somewhere before an e»
Expected output:
Out[]=
42.6Make any 2-letter word uppercase in the first sentence from the Wikipedia article on computers. »
Sample expected output:
Out[]=
42.7Make a labeled bar chart of the number of countries whose TextString names start with each possible letter. »
Sample expected output:
Out[]=
42.8Find simpler code for Grid[Table[StringJoin[TextString[i], "^", TextString[j], "=", TextString[i^j]], {i, 5}, {j, 5}]]»
Expected output:
Out[]=
How does one type `` to make a slot in a string template?
Its a pair of what are usually called backquote or backtick characters. On many keyboards, theyre at the top left, along with ~ (tilde).
Can I write rules for understanding natural language?
What does TextString do when things dont have an obvious textual form?
It does its best to make something human readable, but if all else fails, itll fall back on InputForm.
 
Download Notebook Version