查找连续 2 个大写单词 NSString
我正在编写一个 Mac 应用程序,它通过 NSString 并将其所有单词添加到 NSArray (通过根据空格分隔它们)。现在,我已经关闭了整个系统,但我仍然遇到一个小问题:名称(第一个+最后一个)被添加为两个不同的单词,这对我来说很麻烦。
我想了几个解决方案来解决这个问题。我最好的想法是,在实际将单词添加到数组之前,将两个大写单词连接成一行。然后通过if语句判断一个单词是否有两个大写,然后将这个单词拆分并添加为一个单词。但是,我找不到找到连续两个大写单词的方法。
例如,我是否应该使用 RegexKitLite(我不熟悉)来查找连续的两个大写单词?我见过这个问题: 正则表达式提取不在句子开头的大写单词和两个相邻单词,
这似乎有某种相关性,但由于我对正则表达式缺乏理解,我真的不知道这是否正是我想要的 需要。
我也看到过这个: 将 NSString 分离为 NSArray,但允许用引号对单词进行分组 这也很相似,但不完全适合我的需要。
因此,总而言之,有谁知道如何在 NSString 中连接大写单词,或者更好的是,如何在 NSString 中连续查找两个大写单词?
I'm writing a Mac app that goes through an NSString, and adds all its word to an NSArray (by separating them based on whitespace). Now, I've got the whole system down, but I'm still having one little problem: names (first + last), are added as two different words, and that's bothersome to me.
I thought of a couple solutions to fix this. My best idea was to, before actually adding the words to the array, join two words in a row that are capitalized. Then, through an if statement, determine if a word has two capitals in it, and then split the word and add it as one word. However, I can't find a way to find 2 words in a row with capitals.
Should I be using RegexKitLite (which I'm not familiar with), for example, to find two capitalized words in a row? I've seen this question: Regexp to pull capitalized words not at the beginning of sentence and two adjacent words
which seems somehow related, but due to my lack of understand of regular expressions, I don't really know if this is exactly what I need.
I've also seen this: Separating NSString into NSArray, but allowing quotes to group words
which is also similar, yet not exactly adapted to my needs.
So, to conclude, does anyone know how to either join capitalized words in an NSString, or even better, how to find two capitalized words in a row in an NSString ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的目标是 iOS 4.0 或更高版本或 OS 10.7,您可以使用 NSRegularExpression
你会得到类似这样的结果
["三四", "七八分之一"]
If you're targeting iOS 4.0 or greater OR OS 10.7 you can use NSRegularExpression
You'll get something like this
["Three Four", "Seven Eigth"]
您可以在加载结果数组后对其进行第二次传递,以将需要连接的条目附加在一起。
众所周知,名称很难单独与正则表达式匹配,因为名称(名字或姓氏)本身包含空格并非闻所未闻。
You could just do a second pass on the resulting array after it has been loaded to append entries together that need to be joined.
Names are notoriously difficult to match with regular expressions alone, as it is not unheard of for names (first or last) to contain spaces themselves.
[AZ][A-Za-z]* [AZ][A-Za-z]*|[\S]*
http://rubular.com/r/DrOabOAfBr
我已经为您编写了一个正则表达式。这个正则表达式将首先尝试匹配一个名称,然后回退到一个单词,因此您的工作就像将其输入 NSRegularExpression 一样简单,并将所有匹配项作为您的单词或名称加入。
[A-Z][A-Za-z]* [A-Z][A-Za-z]*|[\S]*
http://rubular.com/r/DrOabOAfBr
I've written a regular expression for you. This regex will try to match a name first, then fall back to a word, so your job is as simple as feeding this into NSRegularExpression, and take all the matches as your words, or names joined.