NSString simple pattern matching

Posted by SirRatty on Stack Overflow See other posts from Stack Overflow or by SirRatty
Published on 2010-06-17T04:09:51Z Indexed on 2010/06/17 4:13 UTC
Read the original article Hit count: 295

Filed under:
|
|
|

Hi all,

Mac OS 10.6, Cocoa project, 10.4 compatibility required.

(Please note: my knowledge of regex is quite slight)

I need to parse NSStrings, for matching cases where the string contains an embedded tag, where the tag format is:

[xxxx]

Where xxxx are random characters.

e.g. "The quick brown [foxy] fox likes sox".

In the above case, I need to grab the string "foxy". (Or nil if no tag is found.)

Each string will only have one tag, and the tag can appear anywhere within the string, or may not appear at all.

Could someone please help with a way to do that, preferably without having to include another library such as RegexKit. Thank you for any help.

© Stack Overflow or respective owner

Related posts about regex

Related posts about cocoa