Created
June 17, 2014 02:31
-
-
Save Reacoder/53459a3bc27819bec722 to your computer and use it in GitHub Desktop.
regex extract string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String extractJsUrl(String originHtml) { | |
// <script src="//s.ytimg.com/yts/jsbin/html5player-zh_CN-vflHa9pzi.js" | |
// name="html5player"></script> | |
final String REGEX = "//s[.]ytimg[.]com/.*html5player.*[.]js"; | |
Pattern p = Pattern.compile(REGEX); | |
Matcher m = p.matcher(originHtml); | |
String jsUrl = null; | |
if (m.find()) { | |
jsUrl = originHtml.substring(m.start(), m.end()); | |
} else { | |
Logger.d("------- can not find jsUrl ------"); | |
int index1 = originHtml.indexOf("name=\"html5player\""); | |
if (index1 > 0) { | |
String sub1 = originHtml.substring(index1 - 80, index1); | |
int start = sub1.indexOf("src=\"//s.") + 5; | |
int end = sub1.indexOf(".js") + 3; | |
jsUrl = sub1.substring(start, end); | |
} | |
} | |
jsUrl = "http:" + jsUrl; | |
Logger.d("------- extractJsUrl ------" + jsUrl); | |
return jsUrl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment