Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you really want to use a regular expression, this following pattern should work:</p> <pre><code>NSString *pattern = @"latlng:\\{lat:([0-9.]+),lng:([0-9.]+)\\}"; </code></pre> <p><code>[0-9.]+</code> matches one or more characters which are a digit or <code>.</code>, and the parentheses around it make it a "capture group", so that the part of the string that matches this part of the pattern is available using <code>rangeAtIndex:</code>.</p> <p>To verify this, I have added the exact input data from your question as a resource file "data.txt" to my test application, and loaded that data with</p> <pre><code>NSURL *url = [[NSBundle mainBundle] URLForResource:@"data.txt" withExtension:nil]; NSError *error; NSString *locationStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&amp;error]; </code></pre> <p>and then parsed the string with the regular expression</p> <pre><code>NSString *pattern = @"latlng:\\{lat:([0-9.]+),lng:([0-9.]+)\\}"; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&amp;error]; NSArray *matches = [regex matchesInString:locationStr options:0 range:NSMakeRange(0, [locationStr length])]; for (NSTextCheckingResult *match in matches) { NSString *lat = [locationStr substringWithRange:[match rangeAtIndex:1]]; NSString *lng = [locationStr substringWithRange:[match rangeAtIndex:2]]; NSLog(@"latidude = %@, longitude = %@", lat, lng); } </code></pre> <p><em>Output:</em></p> <pre><code>latidude = 21.747064, longitude = 72.169678 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload