如何使用NSScanner扫描^[a-zA-Z_][a-zA-Z0-9_]*的格式

发布于 2024-11-18 15:48:26 字数 433 浏览 3 评论 0原文

我试图让我的 NSScanner 尝试扫描以下正则表达式:[a-zA-Z_][a-zA-Z0-9_]*,但遇到困难。

我可以尝试先读取 a-Z_,然后尝试附加 a-Z0-9_。

我想知道是否有更简单/更有效的方法来做到这一点。请告诉我,谢谢。


澄清: 我不想执行正则表达式。我只是想读取一个类似于上面的正则表达式的字符串。看起来类似于 C 风格变量的东西。基本上,任何字母数字单词,但不得以数字开头。


澄清2: 我试图让扫描仪读取([] 表示每个读取令牌): “测试 3”为 [测试,3] “测试3”作为[测试3] “3test”为[3,测试] “_3test”为[_3test] “_3 测试”为 [3,测试] “ 3 3test”为 [, 3, 3, 测试] “ 3 test3”为 [_, 3, test3]

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 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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-11-25 15:48:26

您需要两个字符集:

使用这些将使您能够匹配 Unicode 中的所有字母和数字,而不仅仅是英文字母和数字集。如果您确实只想要那些更小的集合,那么使用 characterSetWithCharactersInString: 和/或 characterSetWithRange:。如果使用后一种方法,则需要创建一个 NSMutableCharacterSet 和 联合另一个字符集到其中。

一旦你有了你的字符集,就很容易 仅扫描字符集中的字符 和然后,如果您愿意,将一个字符串连接到另一个字符串

You'll need two character sets:

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.

终止放荡 2024-11-25 15:48:26

这取决于你想做什么。
如果你想使用正则表达式,我听说过这个框架:
RegexKit

您可以通过字符串、数组、字典等更轻松地应用正则表达式...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文