Objective-C Cocoa 应用程序中的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/422138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:01:50  来源:igfitidea点击:

Regular expressions in an Objective-C Cocoa application

objective-cregexcocoa

提问by dreeves

Initial Googling indicates that there's no built-in way to do regular expressions in an Objective-C Cocoa application.

最初的谷歌搜索表明在 Objective-C Cocoa 应用程序中没有内置的方法来执行正则表达式。

So four questions:

所以四个问题:

  1. Is that really true?

  2. Are you kidding me?

  3. Ok, then is there a nice open-source library you recommend?

  4. What are ways to get close enough without importing a library, perhaps with the NSScanner class?

  1. 真的是这样吗?

  2. 你在跟我开玩笑吗?

  3. 好的,那么您推荐一个不错的开源库吗?

  4. 有什么方法可以在不导入库的情况下足够接近,也许使用 NSScanner 类?

采纳答案by Philip Jespersen

I noticed that as of iOS 4.0 Apple provides a NSRegularExpression class. Additionally, as of 10.7, the class is available under OS X.

我注意到从 iOS 4.0 开始,Apple 提供了一个NSRegularExpression 类。此外,从 10.7 开始,该类在 OS X 下可用。

回答by mfazekas

  1. Yes, there's no regex support in Cocoa. If you're only interested in boolean matching, you can use NSPredicatewhich supports ICU regex syntax. But usually you're interested in the position of the match or position of subexpressions, and you cannot get it with NSPredicate.
  2. As mentioned you can use regex POSIX functions. But they are considered slow, and the regex syntax is limited compared to other solutions (ICU/pcre).
  3. There are many OSS libraries, CocoaDev has an extensive list.
  4. RegExKitLitefor example doesn't requires any libraries, just add the .m and .h to your project.

    (My complaint against RegExKitLite is that it extends NSString via category, but it can be considered as a feature too. Also it uses the nonpublic ICU libraries shipped with the OS, which isn't recommended by Apple.)

  1. 是的,Cocoa 中没有正则表达式支持。如果您只对布尔匹配感兴趣,则可以使用支持 ICU 正则表达式语法的NSPredicate。但通常你对匹配的位置或子表达式的位置感兴趣,而你不能用 NSPredicate 得到它。
  2. 如前所述,您可以使用正则表达式 POSIX 函数。但是它们被认为很慢,并且与其他解决方案(ICU/ pcre)相比,正则表达式的语法是有限的。
  3. 有很多 OSS 库,CocoaDev 有一个广泛的列表
  4. 例如RegExKitLite不需要任何库,只需将 .m 和 .h 添加到您的项目中。

    (我对 RegExKitLite 的抱怨是它通过类别扩展了 NSString,但它也可以被视为一个功能。此外,它使用操作系统附带的非公共 ICU 库,这是 Apple 不推荐的。)

回答by avocade

RegexKit is the best I've found yet. Very Cocoa:y. I'm using the "Lite" version in several of our iPhone apps:

RegexKit 是我发现的最好的。非常可可:是的。我在我们的几个 iPhone 应用程序中使用了“精简版”版本:

sourceforge

源锻造

lingonikorg

林戈尼奥尔格

回答by Adam Wright

You can use the POSIX Regular Expressions library (Yay for a POSIX compliant OS). Try

您可以使用 POSIX 正则表达式库(适用于 POSIX 兼容操作系统)。尝试

man 3 regex

回答by Mark Caufman

The cheap and dirty hack solution that I use to solve REGEX and JSON parsing issues is to create a UIWebView object and inject Javascript function(s) to do the parsing. The javascript function then returns a string of the value (or list of values) I care about. In fact, you can store a small library set of functions customized for particular tasks and then just call them as needed.

我用来解决 REGEX 和 JSON 解析问题的廉价而肮脏的 hack 解决方案是创建一个 UIWebView 对象并注入 Javascript 函数来进行解析。javascript 函数然后返回我关心的值(或值列表)的字符串。事实上,您可以存储一个为特定任务定制的小型函数库,然后根据需要调用它们。

I don't know if it this technique scales to huge volumes of repeated parsing requests, but for quick transactional stuff it gets the job done without depending on any extra external resources or code you might not understand.

我不知道这种技术是否可以扩展到大量重复的解析请求,但是对于快速事务性的东西,它可以在不依赖任何额外的外部资源或您可能不理解的代码的情况下完成工作。

回答by Rob Keniger

I like the AGRegex framework which uses PCRE, handy if you are used to the PCRE syntax. The best version of this framework is the one in the Colloquy IRC client as it has been upgraded to use PCRE 6.7:

我喜欢使用 PCRE 的 AGRegex 框架,如果你习惯了 PCRE 语法,那很方便。该框架的最佳版本是 Colloquy IRC 客户端中的版本,因为它已升级为使用 PCRE 6.7:

http://colloquy.info/project/browser/trunk/Frameworks/AGRegex

http://colloquy.info/project/browser/trunk/Frameworks/AGRegex

It's very lightweight, much more so than RegExKit (although not as capable of course).

它非常轻量级,比 RegExKit 轻得多(尽管当然没有那么强大)。

回答by bbaassssiiee

NSRegularExpressionis available since Mac OS X v10.7 and IOS 4.0.

NSRegularExpression自 Mac OS X v10.7 和 IOS 4.0 起可用。

回答by newtonapple

During my search on this topic I came across CocoaOnigurumawhich uses Oniguruma, the Regular Expression engine behind Ruby1.9 and PHP5. It seems a bit newer compared to the existing OregKit(in Japanese). Not sure how these stack up against other bindings.

在我搜索这个主题的过程中,我遇到了CocoaOniguruma,它使用Oniguruma,Ruby1.9 和 PHP5 背后的正则表达式引擎。与现有的OregKit(日语)相比,它似乎更新了一些。不确定这些与其他绑定如何叠加。

回答by nickeyzzz

Googling alittle, found this library: RegexOnNSString

谷歌搜索,发现这个库: RegexOnN​​SString

Open source library, containing functions like:

开源库,包含以下功能:

-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement caseInsensitive:(BOOL)ignoreCase

and using NSRegularExpressionclass. Quite easy to use and no need to worry about anything.

和使用NSRegularExpression类。非常易于使用,无需担心任何事情。

Please, note that NSRegularExpressionis available since Mac OS X v10.7 and IOS 4.0, as Datasmid mentioned.

请注意NSRegularExpression,正如 Datasmid 所提到的,自 Mac OS X v10.7 和 IOS 4.0 起可用。

回答by Volomike

I make it easy. I add a new C++ file to my Objective C project, rename it as .mm, and then create a standard C++ class inside. Then, I make a static class method in the "public:" section for a C++ function that takes an NSString and returns an NSString (or NSArray, if that's what you want). I then convert NSString to C++ std::string like so:

我让它变得容易。我将一个新的 C++ 文件添加到我的 Objective C 项目中,将其重命名为 .mm,然后在其中创建一个标准的 C++ 类。然后,我在“public:”部分为一个 C++ 函数创建了一个静态类方法,该函数接受一个 NSString 并返回一个 NSString(或 NSArray,如果这是你想要的)。然后我将 NSString 转换为 C++ std::string ,如下所示:

// If anyone knows a more efficient way, let me know in the comments.
// The "if" condition below is because ObjC crashes if converting to
// std::string if the string is nil or empty.
// assume #include <string>
std::string s = "";
if (([sInput != nil]) && (!([sInput isEqualTo:@""]))) {
  std::string sTemp([sInput UTF8String]);
  s = sTemp;
}

From there, I can use regex_replace like so:

从那里,我可以像这样使用 regex_replace:

// assume #include <regex>
std::string sResult = std::regex_replace(sSource,sRegExp,sReplaceWith);

Then, I can convert that std::string back into an NSString with:

然后,我可以将该 std::string 转换回 NSString :

NSString *sResponse2 = @(sResult.c_str());

If you're only using this C++ just for this function, then you may find it suitable to call this file extra.mm (class name Extra) and put this static class method in, and then add other static class methods when the situation arrives where it just makes sense to do it in C++ because it's less hassle in some cases. (There are cases where ObjC does something with less lines of code, and some cases where C++ does it with less lines of code.)

如果你只是为了这个函数而使用这个C++,那么你可能会发现调用这个文件extra.mm(类名Extra)并将这个静态类方法放入其中,然后在情况到来时添加其他静态类方法在 C++ 中这样做是有意义的,因为在某些情况下它不那么麻烦。(有些情况下,ObjC 用较少的代码行做某事,而在某些情况下,C++ 用较少的代码行做某事。)

P.S. Still yet another way with this is to use a .mm file but make an Objective C wrapper around the use of std::string and std::regex_replace() (or regex_match()).

PS 还有另一种方法是使用 .mm 文件,但围绕 std::string 和 std::regex_replace()(或 regex_match())的使用制作 Objective C 包装器。