我正在寻求有关在 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/