1

I'm looking for some help with a regex to be used in an iPhone application.

I'm using 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]";

I have this string which is just BBCode for a forum post. In this case, a quote inside a quote.

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

And that's the regex I'm using to parse it.

It works fine on just normal BBCode without nested quotes. But when the quotes are nested this regex doesn't work as I would like it to.

When running the regex on this particular string it would return something like this:

"[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]

It is incorrectly matching the opening and closing quote tags.

Can anyone see what I'm missing? Thanks.

  • Take a look at: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Andy Jan 09 '13 at 12:02

2 Answers2

1

I've done this regex for you: 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
)

I'm not really sure if this is what you need but I hope it is.

Javier Diaz
  • 1,791
  • 1
  • 17
  • 25
0

You need to anchor your regex both at the beginning and end. Try:

@"^\\[quote author=(.*?) .*?\\](.*?)\\[\\/quote\\]$"
fge
  • 119,121
  • 33
  • 254
  • 329
  • Hmmm...that works but only when I don't have other text around it. –  Jan 09 '13 at 16:49
  • So it won't work for something like this: @"THIS IS TEXT [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]"; –  Jan 09 '13 at 16:50
  • You didn't tell that your comments were potentially embedded within lines... That changes quite a lot. – fge Jan 09 '13 at 16:51