StringExtract

StringExtract["string",n]

extracts the n^(th) block of characters in string, where blocks of characters are defined as delimited by whitespace.

StringExtract["string",{pos1,pos2,}]

extracts blocks at several positions in string.

StringExtract["string",seppos]

takes blocks to be delimited by separators that match sep.

StringExtract["string",pos1,pos2,]

extracts blocks at positions posi, delimiting with whitespace for the lowest level, newlines for the next level, and a successively increasing number of newlines thereafter.

StringExtract["string",sep1pos1,sep2pos2,]

gives a nested list of blocks, with the sepi used as separators for successive levels.

Details

  • The position specifications for blocks can have any of the following forms:
  • nn^(th) block
    -nn^(th) block counting from the end
    {n1,n2,}collection of blocks
    n1;;n2n1^(th) through n2^(th) block
    Allall blocks
  • The separator sep can be a literal string or a string pattern.

Examples

open allclose all

Basic Examples  (3)

Extract the second word from a string:

Extract a sequence of words:

Use -- as a word separator:

Scope  (4)

Extract the last word from a string:

Extract the first and third words:

Extract all whitespace-delimited tokens:

Extract the English words from the text:

Take the first word from each line:

Take the first word from the third line:

Take every word from the third line:

Elements not present in a group yield Missing[]:

Applications  (1)

Extract the first column from a string in CSV (comma-separated values) format:

Extract all columns:

Extract only the first row:

Properties & Relations  (4)

StringExtract["string",pattAll] is equivalent to StringSplit["string",patt]:

StringExtract["string",{pos1,pos2,}] is equivalent to Part[StringSplit["string"],{pos1,pos2,}]:

StringExtract["string",,pos-3,pos-2,pos-1] is equivalent to StringExtract["string",,"\n\n"pos-3,"\n"pos-2,Whitespacepos-1]:

StringExtract can be used on the output of StringRiffle:

Introduced in 2015
 (10.1)