0

i have

NSString *str =  @"<0x112233><0x112233><0x112233>0a";

i want this data to be saved in nsarray

nsarray  : { 0x112233 0x112233 0x112233 }

only save data which is enclosed in "<>" and ignore all other.

i tried regex like this

NSString *str =  @"<0x112233><0x112233><0x112233>0a";

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

NSArray *payloadRanges=[regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];


NSLog(@"data %@",payloadRanges);

but i gives me this output:

data (
    "<NSSimpleRegularExpressionCheckingResult: 0x1ddc5e30>{0, 30}{<NSRegularExpression: 0x1f078710> <(.*)> 0x1}"
)
4
  • 1
    "Extract data using regex from string" - please don't. Use NSScanner or something sane. No regexes for this. Commented Oct 26, 2013 at 11:06
  • 2
    You may wish to make your regular expression non-greedy and ignore the angle brackets - try: (?<=<)(.*?)(?=>) Commented Oct 26, 2013 at 11:13
  • [str stringByReplacingOccurrencesOfString:@"<" withString:@" "]]; Commented Oct 26, 2013 at 11:13
  • @Martin R answer solved my problem . thanks kumar kl Commented Oct 26, 2013 at 11:44

4 Answers 4

5

(As Pat Teen said in a comment), your pattern is greedy, which means that the first match gets as much as possible, which is the entire string.

You can fix that by changing the pattern @"<(.*)>" to @"<(.*?)>".

Then payloadRanges is an array of three NSTextCheckingResult objects:

(
    "<NSSimpleRegularExpressionCheckingResult: 0x10010af40>{0, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}",
    "<NSSimpleRegularExpressionCheckingResult: 0x10010c210>{10, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}",
    "<NSSimpleRegularExpressionCheckingResult: 0x10010c250>{20, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}"
)

Now you have to extract the substring matching the (...) group from each result:

NSMutableArray *data = [NSMutableArray array];
for (NSTextCheckingResult *result in payloadRanges) {
    [data addObject:[str substringWithRange:[result rangeAtIndex:1]]];
}
NSLog(@"data %@",data);

Output:

data (
    0x112233,
    0x112233,
    0x112233
)
Sign up to request clarification or add additional context in comments.

Comments

1

Cool , Try this simple line of manually hardcoded without using of any reg exper.:

NSString *convert =[str stringByReplacingOccurrencesOfString:@"<" withString:@""];

    convert= [convert stringByReplacingOccurrencesOfString:@">" withString:@" "];
    NSLog(@"con %@",convert);
    NSMutableArray *array = (NSMutableArray *)[convert componentsSeparatedByString:@" "];
    NSLog(@"%@" ,array);

    for(NSString *str in array){
        if([str length]<4){
            [array removeObject:str];
        }

    }
    NSLog(@"%@",array);

6 Comments

put the loop for the array . and check for every object of it has minimum length if not just remove the object from the array.
( "", 0x112233, "", 0x112233, "", 0x112233, 0a ) and some empty strings also
con 0x112233 0x112233 0x1122330a 2013-10-26 16:35:48.896 GCDtest1[729:907] ( "", 0x112233, 0x112233, 0x1122330a ) no no 0a should be ignored
currently 0a is at last position but it can be anywhere in string
0x112233, whatever you want that should be the legnth arround 8 .. right
|
0

Try this:-

NSString *str =  @"<0x112233><0x112233><0x112233>0a";
NSArray *arr = [str componentsSeparatedByString:@"<"];

    NSString *str1 = [arr lastObject];

    NSArray *arr1 = [str1 componentsSeparatedByString:@">"];

    NSLog(@"array is %@",arr1);

    NSString *str2 = [[arr1 objectAtIndex:0] substringFromIndex:1];

NSString *str3=[str2 stringByReplacingOccurrencesOfString:@"0a" withString:@""];
NSLog(@"str3 is %@",str3);
[yourArray addObject:str3];

Comments

0

Just another regex that should work with your requirements:

<([^>]*)>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.