我正在寻找一些在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]它与开始和结束引号标记不正确匹配。
有人能看到我错过了什么吗?谢谢。
发布于 2013-01-09 20:27:34
我已经为您完成了这个正则表达式: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
)我不太确定这是否是你需要的,但我希望是。
发布于 2013-01-09 19:52:14
您需要在开头和结尾锚定正则表达式。尝试:
@"^\\[quote author=(.*?) .*?\\](.*?)\\[\\/quote\\]$"https://stackoverflow.com/questions/14234089
复制相似问题