Java实现正则匹配出script标签中字符串包含某个几个单词的所有行,以及编码转换

/*取得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();  
        }



正则表达式验证 java script 验证大全 部分: 下面都是我收集的一些比较常用的正则表达式,因为平常可能在表单验证的时候,用到的比较多。特发来,让各位朋友共同使用。呵呵。 匹配文字符的正则表达式: [u4e00-u9fa5] 评注:匹配文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) 匹配空白正则表达式:ns*r 评注:可以用来删除空白 匹配HTML标记的正则表达式:]*>.*?| 评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力 匹配首尾空白字符的正则表达式:^s*|s*$ 评注:可以用来删除尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式 匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 评注:表单验证时很实用 匹配网址URL的正则表达式:[a-zA-z]+://[^s]* 评注:网上流传的版本功能很有限,上面这个基本可以满足需求 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 评注:表单验证时很实用 匹配国内电话号码:d{3}-d{8}|d{4}-d{7} 评注:匹配形式如 0511-4405222 或 021-87888822 匹配腾讯QQ号:[1-9][0-9]{4,} 评注:腾讯QQ号从10000开始 等等..............................
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值