So I get a string result from a system like this, which I have to capture all the hex parts, excluding the 0x
:
[System Info] 2.20.02 2.20.02 - Extended Data:
0xAC, 0x4D, 0xDE, 0x04, 0xA4, 0x10, 0x73, 0x89, 0xDF, 0xFF, 0x01, 0x01, 0x01, 0xDF, 0x5A, 0x10,
0x34, 0x37, 0x35, 0x36, 0x33, 0xC1, 0x10, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x37, 0x38, 0x31, 0x32,
0x9F, 0xDD, 0x01, 0xB5, 0x42, 0x03, 0x45, 0x56, 0x33, 0x2F, 0x02, 0x06, 0x00, 0x00, 0x00, 0x00,
0x00, 0x15, 0xA3, 0x21, 0x03, 0x09, 0x51, 0x09, 0x9A, 0xE5, 0x16, 0x12, 0x21, 0x9F, 0x34, 0x03,
0x03, 0x1E, 0x03, 0xCE, 0x04, 0x00, 0x12, 0x00, 0x00, 0xDF, 0xFF, 0x02, 0x01, 0x1A,
I have created a function which can help me extract substrings into an array:
+ (NSArray *) regexPattern:(NSString *)pattern toExtract:(NSString *)string{
NSError *error;
NSRegularExpression * regexp = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive error:&error];
if (error == nil) { return nil; }
NSMutableArray * matches = [[regexp matchesInString:string options:0 range:NSMakeRange(0, [string length])] mutableCopy];
[matches removeObjectAtIndex:0]; // because it contains all the string.
NSMutableArray * result = [[NSMutableArray alloc] init];
for (NSTextCheckingResult * match in matches) {
[result addObject:[string substringWithRange:[match range]]];
}
[matches release];
return result;
}
But now the problem is the regex. I have tried to use capture group ()
to capture only the hex part using this pattern: 0x(..),
. This pattern capture the whole 0xFD,
instead of just FD
. If I use ([\dA-F]){2}
, I can get all the hex, but I also capture 20
and 02
from 2.20.02 2.20.02
, which I don't want to. Some website told me that I will only get the data between the capture brackets, but that's not the case. Can somebody help? Thanks.