介绍
public class TextUtil {
public static String getLink(String text) {
Pattern pattern = Pattern.compile("链接:(.*)");
Matcher m = pattern.matcher(text);
String str = "";
if (m.find()) {
str = m.group(1);
}
return str;
}
public static String getCode(String text) {
Pattern pattern = Pattern.compile("提取码:(.*)");
Matcher m = pattern.matcher(text);
String str = "";
if (m.find()) {
str = m.group(1);
}
return str;
}
}
public class TextUtilTest {
@Test
public void getLink() throws Exception {
String text = "链接:https://pan.baidu.com/s/14Ohd4jLuMWLRtqIt6eUNKg\n" +
"提取码:5dlw\n" +
"复制这段内容后打开百度网盘手机App,操作更方便哦";
String str = TextUtil.getLink(text);
assertEquals("https://pan.baidu.com/s/14Ohd4jLuMWLRtqIt6eUNKg", str);
}
@Test
public void getLink1() throws Exception {
String text = "可以的";
String str = TextUtil.getLink(text);
assertEquals("", str);
}
@Test
public void getCode() throws Exception {
String text = "链接:https://pan.baidu.com/s/14Ohd4jLuMWLRtqIt6eUNKg\n" +
"提取码:5dlw\n" +
"复制这段内容后打开百度网盘手机App,操作更方便哦";
String str = TextUtil.getCode(text);
assertEquals("5dlw", str);
}
}
参考博客
[1]