0

I'm trying to get all the XML out of certain tags (not just values but everything including more XML between two open and close tags) and I figured I'd use a regular expression.

My code looks like this

NSError *error2 = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<PayloadPost>(.*)</PayloadPost>"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error2];
if (error2)
{
    NSLog(@"Error %@", [error2 description]);
}

NSArray *payloadRanges=[regex matchesInString:theXMLString options:0 range:NSMakeRange(0, [theXMLString length])];
int size=[payloadRanges count];
printf("Size %d",size);

Basically I need everything in the "PayloadPost"s and yet it keeps giving me nothing. I keep getting 0 for size and I know the XML has 3 instances in it.

Can anyone help me out?

Moshe
  • 57,511
  • 78
  • 272
  • 425
user1515993
  • 69
  • 1
  • 7

1 Answers1

1

make sure you set the multiline flag, https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSRegularExpressionDotMatchesLineSeparators

otherwise the '.' character will not match the line break and your regex will match

<PayloadPost>something</PayloadPost>

but not

<PayloadPost>
something
</PayloadPost>
Qnan
  • 3,714
  • 18
  • 15
  • Well that worked, after I added in a question mark after the asterisk so it would be non-greedy. Now all I need to do is figure out how to make the strings not include "" and " – user1515993 Aug 09 '12 at 22:24
  • check this http://stackoverflow.com/questions/6822356/capture-groups-not-working-in-nsregularexpression?rq=1 – Qnan Aug 09 '12 at 22:28
  • "more than one" option?.. what do you mean, exactly? – Qnan Aug 09 '12 at 23:15
  • having say both the dotmatcheslineseparators and the caseinsensitive options for the same NSRegularExpression. – user1515993 Aug 10 '12 at 00:12
  • I assume they should be combined using the bitwise OR operator, as in `NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators` – Qnan Aug 10 '12 at 08:26