You can solve this problem using a regular expression and NSRegularExpression
.
Construct a pattern which matches the text between brackets. For example the pattern @"\\[([^]]*)]"
matches an opening bracket \\[
– the backslash is required to treat the bracket as a literal character, zero or more characters except a closing bracket [^]]*
, groups that text so it referred to ([^]]*)
, and a closing bracket ]
. You can build up a pattern which matches comma separated sequences of bracket text, or just look for multiple matches of bracketed text – depending on your requirements.
Once you’ve created your regular expression, using the methodregularExpressionWithPattern:options:error:
, the method matchesInString:options:range:
can be used obtain an array of all the matches. You can process this array to produce your required array of strings.
1
solved Objective C: split text between brackets