开发者

get particular string using regex in java

开发者 https://www.devze.com 2022-12-25 13:39 出处:网络
i want how to code for get only link from string using regex or anyothers. here the following is java code:

i want how to code for get only link from string using regex or anyothers.

here the following is java code:

String aas =  "window.open("+"\""+"http://www.example.com/jscript/jex5.htm"+"\""+")"+"\n"+"window.open("+"\""+"http://www.example.com/jscript/jex5.htm"+"\""+")";

how to get t开发者_Go百科he link http://www.example.com/jscript/jex5.htm

thanks and advance


The Regex

(?<=window.open\(")[^"]*(?="\))

matches the link in the string you have given. Properly escaped it reads

"(?<=window.open\\(\")[^\"]*(?=\"\\))"


This will print out the first URL contained in the string that starts with "http://":

  public static void main(String[] args) throws Exception {
    String javascriptString = "window.open(" + "\"" + "http://www.example.com/jscript/jex5.htm" + "\"" + ")" + "\n" + "window.open(" + "\""
        + "http://www.example.com/jscript/jex5.htm" + "\"" + ")";

    Pattern pattern = Pattern.compile(".*(http://.*)\".*\n.*");
    Matcher m = pattern.matcher(javascriptString);

    if (m.matches()) {
      System.out.println(m.group(1));
    }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号