I need to split the text (NSString), that looks like this:
[text],[more text here].
How do I store in a String[] the two texts between brackets, separated by a ","?
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.