Skip to content

Instantly share code, notes, and snippets.

@Reacoder
Created June 17, 2014 02:31
Show Gist options
  • Save Reacoder/53459a3bc27819bec722 to your computer and use it in GitHub Desktop.
Save Reacoder/53459a3bc27819bec722 to your computer and use it in GitHub Desktop.
regex extract string.
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