get particular string using regex in java
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));
}
}
精彩评论