StringMatchQ["string",patt]
tests whether "string" matches the string pattern patt.
StringMatchQ["string",RegularExpression["regex"]]
tests whether "string" matches the specified regular expression.
StringMatchQ["string",{patt1,patt2,…}]
test whether "string" matches any of the patti.
StringMatchQ[{"string1","string2",…},patt]
gives the list of the results for each of the "stringi".
StringMatchQ[patt]
represents an operator form of StringMatchQ that can be applied to an expression.
StringMatchQ
StringMatchQ["string",patt]
tests whether "string" matches the string pattern patt.
StringMatchQ["string",RegularExpression["regex"]]
tests whether "string" matches the specified regular expression.
StringMatchQ["string",{patt1,patt2,…}]
test whether "string" matches any of the patti.
StringMatchQ[{"string1","string2",…},patt]
gives the list of the results for each of the "stringi".
StringMatchQ[patt]
represents an operator form of StringMatchQ that can be applied to an expression.
Details and Options
- StringMatchQ allows both ordinary StringExpression string patterns, as well as abbreviated string patterns containing the following metacharacters:
-
* zero or more characters @ one or more characters, excluding uppercase letters \\*, etc. literal *, etc. - Verbatim["p"] specifies the verbatim string "p", with * and @ treated literally.
- Setting the option IgnoreCase->True makes StringMatchQ treat lowercase and uppercase letters as equivalent.
- Setting the option SpellingCorrection->True makes StringMatchQ allow strings to match even if a small fraction of their characters are different.
- StringMatchQ[form][expr] is equivalent to StringMatchQ[expr,form].
- StringMatchQ[BioSequence["type","seq"],patt] checks the string "seq" against patt. In this case, degenerate letters in patt are interpreted as wildcard patterns based on the type of biomolecular sequence. Use Verbatim["patt"] to match degenerate letters literally.
- The documentation for BioSequence lists the degenerate letters supported by each type of biomolecular sequence.
- If the biomolecular sequence operated upon by StringMatchQ is circular, wraparound matches are possible.
Examples
open all close allBasic Examples (4)
Test whether a string matches the pattern:
StringMatchQ["apppbb", "p" ~~ ___ ~~ "b"]StringMatchQ["apppbb", "a" ~~ ___ ~~ "b"]Use abbreviated string patterns:
StringMatchQ["apppbb", "a*b"]Give a list of strings as input:
StringMatchQ[{"a", "b", "ab", "abcd", "bcde"}, "a" ~~ ___]Use the operator form of StringMatchQ:
StringMatchQ["a*b"]["apppbb"]Use the operator form to select strings:
Select[{"CAC1", "CTG1", "ACT1", "CGA1", "CTC1"}, StringMatchQ["*G*"]]Scope (10)
StringMatchQ["tester", "t" ~~ __ ~~ "t"]StringMatchQ["acggtaagc", Characters["acgt"]..]StringMatchQ["MATCH", "*H"]StringMatchQ["acggtaagc", RegularExpression["[acgt]+"]]Mix regular expressions and string patterns:
StringMatchQ["abc 123 a", RegularExpression["a.*"] ~~ DigitCharacter..]Use pattern matching for dates:
StringMatchQ["31/12/2003", DatePattern[{"Day", "Month", "Year"}]]Test for occurrences of either pattern:
StringMatchQ["abcdabcdcd", {"a*", "*d"}]StringMatchQ["abcdabcdcd", "a*" | "*d"]StringMatchQ automatically threads over lists of strings:
StringMatchQ[{"ability", "listable", "argument"}, "a" ~~ __ ~~ "t" ~~ ___]Check whether a biomolecular sequence will match a string pattern:
StringMatchQ[BioSequence["DNA", "ATG"], "AT" ~~ ("C" | "G")]Use a wildcard in the pattern compared against a biomolecular sequence:
StringMatchQ[BioSequence["DNA", "ATGGC"], "AN" ~~ ___]The "N" is a degenerate letter only in biomolecular sequences:
StringMatchQ["ATGGC", "AN" ~~ ___]Use Verbatim to avoid a degenerate match:
StringMatchQ[BioSequence["DNA", "ATGGC"], ___ ~~ Verbatim["N"] ~~ ___]StringMatchQ[BioSequence["DNA", "ATGNC"], ___ ~~ Verbatim["N"] ~~ ___]Circular sequences support wraparound matches:
StringMatchQ[BioSequence["CircularDNA", "ACGCT"], "TA" ~~ ("S"..)]StringMatchQ[BioSequence["CircularDNA", "ACGCT"], "AT" ~~ ("S"..)]Options (3)
IgnoreCase (2)
Use IgnoreCaseTrue to check whether the string matches regardless of case:
StringMatchQ["acggtATTCaagc", __ ~~ "aT" ~~ __, IgnoreCase -> True]The operator representation of a case-independent check:
StringMatchQ["ac", IgnoreCase -> True]["AC"]SpellingCorrection (1)
The option SpellingCorrection -> True allows automatic spelling correction:
StringMatchQ["commit", "comit", SpellingCorrection -> True]Applications (1)
Properties & Relations (9)
For literal strings, SameQ is equivalent to StringMatchQ:
"abbaab" === "abbab"StringMatchQ["abbaab", "abbab"]For fast extraction of elements in a list, use Pick:
dna = Table[StringJoin[RandomChoice[{"a", "c", "g", "t"}, 10]], {50000}];Pick[dna, StringMatchQ[dna, "a" ~~ ___ ~~ "ggg" ~~ ___]]//Timing//FirstCompare to Select:
Select[dna, StringMatchQ[#, "a" ~~ ___ ~~ "ggg" ~~ ___]&]//Timing//FirstStringMatchQ threads over lists of strings in the first argument:
StringMatchQ[{"a", "Z", "9", "."}, LetterCharacter]StringMatchQ["string",{patt1,patt2,…}] is equivalent to StringMatchQ["string",Alternatives[patt1,patt2,…]]:
StringMatchQ["acd", {"a*", "b*"}]StringMatchQ["acd", "a*" | "b*"]Use StringFreeQ to check no substring matches a pattern:
StringFreeQ["commit", "om" ~~ __]StringMatchQ generally returns False when StringFreeQ is True:
StringFreeQ["commit", "am" ~~ __]StringMatchQ["commit", "am" ~~ __]Use StringContainsQ to check if a substring matches a pattern:
StringContainsQ["commit", "om" ~~ __]StringMatchQ checks whether a whole string matches a pattern:
StringMatchQ["commit", "om" ~~ __]StringMatchQ generally returns False when StringContainsQ is False:
StringContainsQ["commit", "am" ~~ __]StringMatchQ["commit", "am" ~~ __]StringContainsQ["string",patt] is equivalent to StringMatchQ["string",___~~patt~~___]:
StringContainsQ[{"commit", "giveup"}, "om"]StringMatchQ[{"commit", "giveup"}, ___ ~~ "om" ~~ ___]StringStartsQ["string",patt] is equivalent to StringMatchQ["string",StartOfString~~patt~~___]:
StringStartsQ[{"commit", "omit"}, "om"]StringMatchQ[{"commit", "omit"}, StartOfString ~~ "om" ~~ ___]StringEndsQ["string",patt] is equivalent to StringMatchQ["string",___~~patt~~EndOfString]:
StringEndsQ[{"omit", "omen"}, "it"]StringMatchQ[{"omit", "omen"}, ___ ~~ "it" ~~ EndOfString]Possible Issues (3)
StringMatchQ supports metacharacters:
StringMatchQ["aaa", "a*a"]Most other string functions do not, which can lead to apparently contradictory results:
StringFreeQ[ "aaa", "a*a"]Wrap string patterns in Verbatim to disable metacharacters, giving consistency between the functions:
StringMatchQ["aaa", Verbatim["a*a"]]StringMatchQ does not work on strings with embedded formatting information:
StringMatchQ["StyleBox[teStyleBox["ste",FontColor->RGBColor[1, 0, 0]]r]", "t" ~~ __]StringMatchQ["tester", "t" ~~ __]Spelling correction only works for letter string patterns:
StringMatchQ["abcde", "A@", SpellingCorrection -> True]See Also
StringContainsQ StringFreeQ StringPosition StringCases StringCount Equal Names MatchQ StringStartsQ StringEndsQ StringExpression RegularExpression EditDistance
Function Repository: StringOverlapsQ StringPatternQ
Tech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2004 (5.1) ▪ 2016 (10.4) ▪ 2020 (12.2)
Text
Wolfram Research (1988), StringMatchQ, Wolfram Language function, https://reference.wolfram.com/language/ref/StringMatchQ.html (updated 2020).
CMS
Wolfram Language. 1988. "StringMatchQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/StringMatchQ.html.
APA
Wolfram Language. (1988). StringMatchQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringMatchQ.html
BibTeX
@misc{reference.wolfram_2026_stringmatchq, author="Wolfram Research", title="{StringMatchQ}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringMatchQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringmatchq, organization={Wolfram Research}, title={StringMatchQ}, year={2020}, url={https://reference.wolfram.com/language/ref/StringMatchQ.html}, note=[Accessed: 13-June-2026]}