WolframLang: String Match
Check a Pattern Exist
These functions take a string pattern. either Regular Expression or String Expression.
StringStartsQ
StringEndsQ
StringMatchQ
-
StringMatchQ["abc", RegularExpression[ "..." ] ] === True (* must match the whole string *) StringMatchQ["abc", RegularExpression[ "." ] ] === False
StringContainsQ
-
StringContainsQ[ "some thing", "th", IgnoreCase -> True ]
StringFreeQ
Get Substring by Pattern
StringCases[str, pattern]
-
Get a list of substrings that match a pattern.
〔see Regular Expression〕
(* extract email address *) x = "emails john@gmail.com and mary441@yahoo.com etc"; StringCases[ x, RegularExpression[ "[a-z0-9]+@[a-z0-9]+\\.com" ], IgnoreCase -> True] (* {john@gmail.com, mary441@yahoo.com} *)
StringCases[str, pattern :> replaceBy]
-
Get a list of substrings that match a pattern, and replace each by replaceBy
(* get total number of colored balls *) xx = "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green"; StringCases[xx, RegularExpression["([0-9]+) (red|green|blue)"] :> ToExpression[ "$1" ] ] (* {8, 6, 20, 5, 4, 13} *)
StringPosition
-
get a list of beginning and end positions.
StringPosition[ "x331 x71 59", RegularExpression["x[0-9]+"] ] (* {{1, 4}, {6, 8}} *)
StringCount
-
get number of occurrence of a string pattern.
StringCount[ "x331 x71 59", RegularExpression["x[0-9]+"] ] (* 2 *)
WolframLang String
- WolframLang: String
- WolframLang: String Functions
- WolframLang: Get SubString
- WolframLang: Convert String
- WolframLang: Format String
- WolframLang: String Match
- WolframLang: String Replace
- WolframLang: Regular Expression
- WolframLang: String Expression
- WolframLang: StringExpression Pattern Syntax
- WolframLang: RegularExpression vs StringExpression