neutrino
neutrino

Reputation: 441

Regex help in objective c

<NSSimpleRegularExpressionCheckingResult: 0x100217890>{0, 4}{<NSRegularExpression: 0x100214700> test 0x1}

This is one element in the array which stores the result of a regular expression search.

I've got what I want: 'test'. I don't however want all the stuff around it ie

<NSSimpleRegularExpressionCheckingResult: 0x100217890>{0, 4}{<NSRegularExpression: 0x100214700> etc

I've got a feeling I'm going to have to send something to this element ie

[element stringValue];

but I need a little help discovering what that is..

My full code is below:

NSString *test = @"test 123 test";
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"test" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray* result = [regex matchesInString:test options:0 range:NSMakeRange(0, [test length])];
NSLog(@" %@", [result objectAtIndex:0]);

which puts out

 <NSSimpleRegularExpressionCheckingResult: 0x105b17890>{0, 4}{<NSRegularExpression: 0x105b14700> test 0x1}

Thanks!!

Upvotes: 2

Views: 1047

Answers (2)

Protheus
Protheus

Reputation: 3068

[regex matchesInString...

gives you NSArray of NSTextCheckingResults. Maybe you'd want to use firstMatchInSting:options:range: It will give you NSTextCheckingResult, from which you can get range (NSRange) which you apply to your string with substringWithRange: method.

I hope you can understand. If not - I'll explain more carefully. Nevertheless, read NSRegularExpression reference and NSTextCheckingResult reference

Upvotes: 3

neutrino
neutrino

Reputation: 441

Okay. I'm making process. If I send 'regularExpression' to the element, it narrow the result down to

<NSRegularExpression: 0x106214700> test 0x1

I'm aware that this is the print-out of an object but I am still unsure how to isolate the text!

Upvotes: 0

Related Questions