Java实现正则匹配出script标签中字符串包含某个几个单词的所有行,以及编码转换
2017-03-24 16:53
381 查看
/*取得script下面的JS变量*/
Elements scriptinfo = document.getElementsByTag("script").eq(21);
List<String> list=new ArrayList<String>();
Map<String,String> PopWindowInfo = new LinkedHashMap<String,String>();
/*循环遍历script下面的JS变量*/
for (Element script : scriptinfo) {
Pattern p=Pattern.compile("((impression|more_desc|address|phone:)"+".+?)(\",)");
Pattern p2=Pattern.compile("((ticket_info|bestvisittime|besttime|open_time_desc)"+".+?);");
String sss=script.data().toString();
// new JsonPathSelector("$..title").select(page.getRawText());
System.out.println(sss);
Matcher m = p.matcher(sss);//与字符串匹配
String arrq=null;
while(m.find()){
if(m.group(1).split(":\"").length>1){
arrq= m.group(1).split(":\"")[1];
}else{
arrq="";
}
PopWindowInfo.put(m.group(1).split(":\"")[0], convert(arrq));
// System.out.println(m.group(1).split(":\"")[0]+":"+new String(arrq.getBytes("ISO8859-1"), "GBK"));
System.out.println(m.group(1).split(":\"")[0]+":"+ convert(arrq));
}
Matcher m2 = p2.matcher(sss);//与字符串匹配
String arrqq=null;
String [] stt = null;
while(m2.find()){
String st="\',\\{text:\"";
if(m2.group(1).split(st).length>1){
// System.out.println(m2.group(1).split(st)[1]);
stt= m2.group(1).split(st)[1].split("\"}\\)");
if(stt.length !=0){
arrqq =stt[0];
}else{
arrqq="";
}
PopWindowInfo.put(m2.group(1).split(st)[0],convert(arrqq));
// System.out.println(m.group(1).split(":\"")[0]+":"+new String(arrq.getBytes("ISO8859-1"), "GBK"));
System.out.println(m2.group(1).split(st)[0]+":"+convert(arrqq));
}
}
}
public static String convert(String utfString){
StringBuilder sb = new StringBuilder();
int i = -1;
int pos = 0;
while((i=utfString.indexOf("\\u", pos)) != -1){
sb.append(utfString.substring(pos, i));
if(i+5 < utfString.length()){
pos = i+6;
sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));
}
}
return sb.toString();
}
Elements scriptinfo = document.getElementsByTag("script").eq(21);
List<String> list=new ArrayList<String>();
Map<String,String> PopWindowInfo = new LinkedHashMap<String,String>();
/*循环遍历script下面的JS变量*/
for (Element script : scriptinfo) {
Pattern p=Pattern.compile("((impression|more_desc|address|phone:)"+".+?)(\",)");
Pattern p2=Pattern.compile("((ticket_info|bestvisittime|besttime|open_time_desc)"+".+?);");
String sss=script.data().toString();
// new JsonPathSelector("$..title").select(page.getRawText());
System.out.println(sss);
Matcher m = p.matcher(sss);//与字符串匹配
String arrq=null;
while(m.find()){
if(m.group(1).split(":\"").length>1){
arrq= m.group(1).split(":\"")[1];
}else{
arrq="";
}
PopWindowInfo.put(m.group(1).split(":\"")[0], convert(arrq));
// System.out.println(m.group(1).split(":\"")[0]+":"+new String(arrq.getBytes("ISO8859-1"), "GBK"));
System.out.println(m.group(1).split(":\"")[0]+":"+ convert(arrq));
}
Matcher m2 = p2.matcher(sss);//与字符串匹配
String arrqq=null;
String [] stt = null;
while(m2.find()){
String st="\',\\{text:\"";
if(m2.group(1).split(st).length>1){
// System.out.println(m2.group(1).split(st)[1]);
stt= m2.group(1).split(st)[1].split("\"}\\)");
if(stt.length !=0){
arrqq =stt[0];
}else{
arrqq="";
}
PopWindowInfo.put(m2.group(1).split(st)[0],convert(arrqq));
// System.out.println(m.group(1).split(":\"")[0]+":"+new String(arrq.getBytes("ISO8859-1"), "GBK"));
System.out.println(m2.group(1).split(st)[0]+":"+convert(arrqq));
}
}
}
public static String convert(String utfString){
StringBuilder sb = new StringBuilder();
int i = -1;
int pos = 0;
while((i=utfString.indexOf("\\u", pos)) != -1){
sb.append(utfString.substring(pos, i));
if(i+5 < utfString.length()){
pos = i+6;
sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));
}
}
return sb.toString();
}
相关文章推荐
- 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
- 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。
- 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字
- java正则匹配字符串中不包含一个或几个特定的字符串
- 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字
- java 正则表达式查找某段字符串中所有小写字母开头的单词并统计次数,按出现次数排序
- java: 正则匹配字符串中多个目标子串(贪婪匹配的模拟实现)
- Java查找一个字符串中所有匹配某正则的子串
- java正则匹配实现字符串2014-9-10 22:34 对应加上n秒后的时间
- java使用正则表达式获取字符串中的所有英文单词或数字
- 【Java】正则表达式匹配多单词任意顺序组合的字符串
- 正则表达式匹配所有不包含 css 的 html 标签
- Java实现——字符串分割以及复制目录下的所有文件
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- Java实现——字符串分割以及复制目录下的所有文件
- 使用boost库的正则表达式regex实现从一篇文档中找出所有匹配的字符串
- 正则表达式不包含某几个字符串以及反向引用~~记录下
- java中字符串处理、串联和转换的几个常用方法,以及如果需要自己编程实现的具体实施步骤。
- java正则表达式获取字符串中的所有英文单词或数字
- 请教一个正则表达式,匹配所有Html标签外部的指定字符串