StringCases

StringCases["string",patt]

gives a list of the substrings in "string" that match the string expression patt.

StringCases["string",lhsrhs]

gives a list of the values of rhs corresponding to the substrings that match the string expression lhs.

StringCases["string",p,n]

includes only the first n substrings that match.

StringCases["string",{p1,p2,}]

gives substrings that match any of the pi.

StringCases[{s1,s2,},p]

gives the list of results for each of the si.

StringCases[patt]

represents an operator form of StringCases that can be applied to an expression.

Details and Options

Background & Context

Examples

open allclose all

Basic Examples  (3)

Find the substrings matching a pattern:

Return only the named wild-card character in each substring:

Use the operator form of StringCases:

Scope  (8)

Use string patterns:

Use a regular expression:

Use pattern matching for dates:

Mixed regular expressions and string patterns:

Rules to extract values corresponding to matching substrings:

Include only the two first strings that match:

Occurrences in either substring:

StringCases automatically threads over lists of strings:

Options  (2)

IgnoreCase  (1)

Find all substrings "cat", including use of uppercase letters:

Overlaps  (1)

Find all runs of two or more letters starting with the letter "a":

Allow overlaps between the substrings:

Allow multiple substrings to start at the same character as well:

Applications  (3)

Extract phone numbers from a text:

Find the sequence of section numbers in the US Constitution:

Find the numbers of amendments to the Constitution:

Primes whose digits are all consecutive:

Properties & Relations  (2)

StringCount gives the number of matching substrings:

The length of matching substrings:

Use StringPosition to get the position of matching substrings:

Check:

Possible Issues  (1)

Use :> rather than -> if the right-hand side of a rule contains string operations:

Using -> in such cases leads to immediate evaluation and possible error messages:

Neat Examples  (1)

Find the sequence of words in the US Constitution:

Total number of words:

Number of distinct words:

Logarithmic frequency distribution of words:

Ten most common words:

Introduced in 2004
 (5.1)
 |
Updated in 2015
 (10.1)