开发者

How to use NSScanner to scan in the format of ^[a-zA-Z_][a-zA-Z0-9_]*

I'm trying to have my NSScanner attempt to scan the following regexp: [a-zA-Z_][a-zA-Z0-9_]*, but am having difficulty.

I can try to read a-Z_ first, then try to append a-Z0-9_.

I'm wondering if there is an easier / more efficient way of doing this. Please let me know, thanks.


Clarification: I'm not trying to execute 开发者_Python百科a regular expression. I'm just trying to read a string that looks like the above regexp. Something that looks similar to C-style variables. Basically, any alphanumeric word, but must not start with a number.


Clarification 2: I'm trying to have the scanner read ([] indicate each read token): "test 3" as [test, 3] "test3" as [test3] "3test" as [3, test] "_3test" as [_3test] "_3 test" as [3, test] " 3 3test" as [, 3, 3, test] " 3 test3" as [_, 3, test3]


You'll need two character sets:

  • [NSCharacterSet letterCharacterSet]
  • [NSCharacterSet alphanumericCharacterSet]

Using these will enable you to match all letters and numbers in Unicode, not just the English alphabet and digit set. If you really do want only those much-smaller sets, they're easy enough to construct using characterSetWithCharactersInString: and/or characterSetWithRange:. If you use the latter method, you'll need to make an NSMutableCharacterSet and union another character set into it.

Once you have your character set, it's easy to scan characters only characters within a set and then, if you want, concatenate one string onto the other.


It depends what you want to do. If you want use regexp, i heard about this framework : RegexKit

You can apply regex more easily trough your strings, array, dict, etc...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜