通过NSRegularExpression获取Javascript函数之间的具体值? [英] Get specific Value between Javascript Function by NSRegularExpression?
问题描述
我正在创建一个用于学习目的的项目:
FOR INFO: c $ c> UIWebView
FOR INFO : I have not used UIWebView
在我的项目中,我有 HTML
> content )。此数据包含有关地点的所有信息(来自google地图)。为了从 HTML
内容获取特定数据,我需要解析 HTML
。我使用 Hpple $解析
。我可以成功解析HTML以获取特定数据(例如名称,地址等),但是我需要从 HTML
c $ c> 解析纬度和经度
HTML 内容,我很困惑如何获取地点的纬度和经度,因为那些是 Javascript
的一部分。我的意思是这些纬度和经度数据在 JavaScript的
函数中可用。
In my project I got HTML
data(content) from server. This data contains all information about places(from google map). For getting specific data from HTML
content I need to parse HTML
. I parse HTML
using Hpple
. And I could successfully parse HTML to get specific data (Such as name,address…etc), but when I need to parse latitude and longitude from HTML
content, I am confused about how to get latitude and longitude of places, because those are part of Javascript
. I mean these latitude and longitude data are available in Javascript's
functions.
服务器:(我的限制是,我只能输入一段JavaScript代码,因为此代码非常长)
Javascript content that I got from Server: (My limitation is that, I can only put piece of javascript code bacause this code is very very long)
function()
{
window.gHomeVPage=
{
title:'Hostel, Bhavnagar, Gujarat, India - Google Maps',url:'/?q\\x3dHostel,+Bhavnagar,+Gujarat,+India\\x26hq\\x3dHostel,\\x26hnear\\x3dBhavnagar,+Gujarat,+India\\x26t\\x3dm\\x26ie\\x3dUTF8',urlViewport:false,ei:'jp8tUb3uNK2ciAeQroGACw',
form:{
selected:'q',
q:{q:'Hostel, Bhavnagar, Gujarat, India',what:'Hostel,',near:'Bhavnagar, Gujarat, India'},d:{saddr:'',daddr:'',dfaddr:'Bhavnagar, Gujarat, India'},geocode:''},
query:{type:'l'},viewport:{center:{lat:21.757528,lng:72.15303},span:{lat:0.034314,lng:0.039956},zoom:14,mapType:'m',source:0},modules:['','strr','pphover','act_s','appiw','rst'],
overlays:{sxcar:false,
markers:
[{id:'A',cid:'7569356420090555589',latlng:{lat:21.747064,lng:72.169678},image:'http://maps.gstatic.com/intl/en_ALL/mapfiles/markers2/circleA.png',sprite:{width:20,height:34,top:0,image:'http://maps.gstatic.com/mapfiles/markers2/red_circle_markers_A_J2.png'},icon_id:'A',ext:{width:20,height:34,shadow:'http://maps.gstatic.com/intl/en_ALL/mapfiles/circle-shadow45.png',shadow_width:37,shadow_height:34,mask:false},drg:true,laddr:'Shree Sahajanand Girls Ptc Hostel, Ghogha Road, Bhavnagar, Gujarat 364001, India',geocode:'CfYWJFjKQj0mFXjVSwEdzjhNBCmN5-3pPlpfOTHF7NFVj8ELaQ',sxti:'Shree Sahajanand Girls Ptc Hostel',name:'Shree Sahajanand Girls Ptc Hostel',infoWindow:{title:'Shree Sahajanand Girls Ptc \\x3cb\\x3eHostel\\x3c/b\\x3e',addressLines:['Ghogha Road','Bhavnagar, Gujarat 364001, India'],phones:[{number:'0278 2562529'}],basics:'\\x3cdiv transclude\\x3d\\x22iw\\x22\\x3e\\x3c/div\\x3e',moreInfo:'more info',place_url:'http://maps.google.com/local_url?dq\\x3dHostel,+Bhavnagar,+Gujarat,+India\\x26q\\x3dhttps://plus.google.com/106028699675431268945/about%3Fhl%3Den\\x26s\\x3dANYYN7mCKtIBT1JPxwi6G2b9gVDdCuVyyA',zrvOk:true,loginUrl:'https://www.google.com/accounts/ServiceLogin?service\\x3dlocal\\x26hl\\x3den\\x26nui\\x3d1\\x26continue\\x3dhttp://maps.google.com/maps/place%3Fcid%3D7569356420090555589%26q%3DHostel,%2BBhavnagar,%2BGujarat,%2BIndia%26t%3Dm%26cd%3D1%26cad%3Dsrc:ppwrev%26ei%3Djp8tUb3uNK2ciAeQroGACw%26action%3Dopenratings',lbcurl:'http://www.google.com/local/add/choice?hl\\x3den\\x26gl\\x3dIN\\x26latlng\\x3d7569356420090555589\\x26q\\x3dHostel,\\x26near\\x3dBhavnagar,+Gujarat,+India',link_jsaction:''},ss:{edit:true,detailseditable:true,deleted:false,rapenabled:true,mmenabled:true},b_s:2,approx:true,elms:[4,1,6,2,12,1,9,1,5,2,11]
}
}
在上面的代码中,我想获得 lat:
和 lon:
来自 latlng:{lat:21.747064 ,lng:72.169678}
Here in above code I want to get value of lat:
and lon:
from latlng:{lat:21.747064,lng:72.169678}
如果是从javascript下载,我googled ,发现我需要使用< a href =https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html =nofollow> NSRegularExpression $ c
For getting it from javascript, I googled and found that I need to use NSRegularExpression
class for get specific matches(of Data) form content.
然后我尝试使用以下代码
Then I tried with following code
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?-imsx:latlng:)" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *arrayOfAllMatches = [regex matchesInString:locationStr options:0 range:NSMakeRange(0, [locationStr length])];
for (NSTextCheckingResult *match in arrayOfAllMatches)
{
NSString* substringForMatch = [locationStr substringWithRange:match.range];
NSLog(@"%@",substringForMatch);
}
我在控制台输出如下:
2013-02-28 11:35:25.051 MapExample[949:13d03] latlng:
2013-02-28 11:35:25.766 MapExample[949:13d03] latlng:
2013-02-28 11:35:26.208 MapExample[949:13d03] latlng:
2013-02-28 11:35:26.799 MapExample[949:13d03] latlng:
2013-02-28 11:35:27.303 MapExample[949:13d03] latlng:
2013-02-28 11:35:27.722 MapExample[949:13d03] latlng:
如何获得搜索节点的内容 NSRegularExpression
?
How can I get content of searched node from NSRegularExpression
?
推荐答案
如果你真的想使用正则表达式,下面的模式应该工作:
If you really want to use a regular expression, this following pattern should work:
NSString *pattern = @"latlng:\\{lat:([0-9.]+),lng:([0-9.]+)\\}";
[0-9。] +
一个或多个字符,这是一个数字或。
,并且它周围的括号使它成为一个捕获组,以便匹配这部分的字符串模式可以使用 rangeAtIndex:
。
[0-9.]+
matches one or more characters which are a digit or .
, 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 rangeAtIndex:
.
要验证这一点,我已经添加了您的问题的确切输入数据资源文件data.txt加载到我的测试应用程序,并用
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
NSURL *url = [[NSBundle mainBundle] URLForResource:@"data.txt" withExtension:nil];
NSError *error;
NSString *locationStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
然后使用正则表达式解析字符串
and then parsed the string with the regular expression
NSString *pattern = @"latlng:\\{lat:([0-9.]+),lng:([0-9.]+)\\}";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&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);
}
输出: b
Output:
latidude = 21.747064, longitude = 72.169678
这篇关于通过NSRegularExpression获取Javascript函数之间的具体值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!