ios - NSRegularExpression 帮助 + iOS

标签 ios regex

我正在寻求有关在 iPhone 应用程序中使用正则表达式的帮助。

我正在使用 NSRegularExpression。

NSString *string = @"[quote author=iffets12345 link=topic=36426.msg388088#msg388088 date=1294820175][quote author=fuzzylogic link=topic=36426.msg387976#msg387976 date=1294802623]Although it wouldn't come up too often in an English essay: MUM not mom!!!![/quote]Haha, EXACTLY![/quote]";

我有这个字符串,它只是论坛帖子的 BBCode。在这种情况下,引号内有引号。

NSRegularExpression *quoteRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[quote author=(.*?) .*?\\](.*?)\\[\\/quote\\]"
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:&error];

这就是我用来解析它的正则表达式。

它在没有嵌套引号的普通 BBCode 上运行良好。但是当引号被嵌套时,这个正则表达式就不能像我希望的那样工作了。

在这个特定字符串上运行正则表达式时,它会返回如下内容:

"[quote author=iffets12345 link=topic=36426.msg388088#msg388088 date=1294820175][quote author=fuzzylogic link=topic=36426.msg387976#msg387976 date=1294802623]Although it wouldn't come up too often in an English essay: MUM not mom!!!![/quote]

它错误地匹配了开始和结束引号标签。

任何人都可以看到我错过了什么吗? 谢谢。

最佳答案

我已经为你做了这个正则表达式:DEMO

(
   \[quote\s+author=([^\[\]]*)
          \s+link  =([^\[\]]*)
          \s+date  =([^\[\]]*)\]  #The [quote author=4543] part
   (?>
       (?<text>[^\[\]]+)          #Here is where I ask for text or another quote inside it
       |
       (?<quote>(?1))             #I say that there can be another quote 
                                  #inside a quote (you just will be able 
                                  #to backreference the author of the first one
   )*
   \[\/quote\]                    #End of the quote text
)

我不确定这是否是您需要的,但我希望是。

关于ios - NSRegularExpression 帮助 + iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14234089/

相关文章:

c# - 正则表达式 C# : Split between two delimiters

ios - Xcode (iOS) - 一键切换 Storyboard

ios - UIActivityViewController 完成处理程序在 tweet 失败时返回成功

ios - Apple map ,栅格覆盖在标签上方 - 解码失败

c - 如何在 sscanf() 中使用 RegEx 以特定方式在 c 中标记字符串?

regex - 用于解析名称/值对的正则表达式

ios - 据报告 View 的尺寸已切换

ios - 如何强制显示 iOS 绿色 CallKit 栏

regex - 再次验证电子邮件地址

javascript - 在电话 01x-xxxxxxxx jquery 上验证