Question

I have a QR code scanner, that scans a URL, then a regex function to strip the "serial" part of the code which i want to sent as a http request.

//scanned code
NSLog(@"Scanned QRCode: %@",[metadataObj stringValue]);
NSString *ScQR = [metadataObj stringValue];

//regex for stripping the serial
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"ticketguid=(.+)($|&)" options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *modifiedString = [regex firstMatchInString:ScQR options:regex range:NSMakeRange(0, [ScQR length])];
NSString *strippedCode = [ScQR substringWithRange:[modifiedString rangeAtIndex:1]];


NSLog(@"Stripped code: %@",strippedCode);
[self validateTicketWithQRCode:strippedCode];

output (expected):

2014-05-09 15:54:15.371  Scanner[2644:4703] Scanned QRCode:  http://www.somewebsite.dk/client/Scanticket.aspx?TicketGUID=593c5515-dd84-4025-a4a5-fdaec8c2af45
2014-05-09 15:54:15.390  Scanner[2644:4703] Stripped code: 593c5515-dd84-4025-a4a5-fdaec8c2af45

as you see, it gets the code, strips the serial part and all is good.

output (30% of the time)

2014-05-09 15:54:19.100  Scanner[2644:4703] Scanned QRCode: http://www.somewebsite.dk/client/Scanticket.aspx?TicketGUID=593c5515-dd84-4025-a4a5-fdaec8c2af45
2014-05-09 15:54:19.100  Scanner[2644:4703] Stripped code: 

it scans the code, runs the exact same operations, but for some reason, the stripped code is empty..

any help appreciated.

Was it helpful?

Solution

Instead of regex use NSString rangeOfString:

To get the stripped QR code:
NSRange range = [ScQR rangeOfString:@"GUID="];
NSString *strippedCode = [ScQR substringFromIndex:range.location+range.length];

That should get you the stripped code as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top