首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSRegularExpression帮助+ iOS

NSRegularExpression帮助+ iOS
EN

Stack Overflow用户
提问于 2013-01-09 19:20:41
回答 2查看 238关注 0票数 1

我正在寻找一些在iPhone应用程序中使用正则表达式的帮助。

我使用的是NSRegularExpression。

代码语言:javascript
复制
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。在本例中,是指引号内的引号。

代码语言:javascript
复制
NSRegularExpression *quoteRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[quote author=(.*?) .*?\\](.*?)\\[\\/quote\\]"
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:&error];

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

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

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

代码语言:javascript
复制
"[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]

它与开始和结束引号标记不正确匹配。

有人能看到我错过了什么吗?谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-01-09 20:27:34

我已经为您完成了这个正则表达式:DEMO

代码语言:javascript
复制
(
   \[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
)

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

票数 1
EN

Stack Overflow用户

发布于 2013-01-09 19:52:14

您需要在开头和结尾锚定正则表达式。尝试:

代码语言:javascript
复制
@"^\\[quote author=(.*?) .*?\\](.*?)\\[\\/quote\\]$"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14234089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档