我有以下字符串,我想从 a href 标记中导出数字 (104321)。我怎样才能得出这个数字。
Hello this is testing string <a href=\"/testing/104321\">Ap</a><img src=\"Image Url" width=\"222\" height=\"149\"/><br/><br/>test\u00e4n p\u00e4\u00e4ll\u00e4 test, test\u00e4, test?
我希望最终的输出是这样的。
String[] strExample= {"testing", "104321","test\u00e4n p\u00e4\u00e4ll\u00e4 test, test\u00e4, test?"};
感谢任何帮助。
最佳答案
您可以尝试使用正则表达式进行简单的模式匹配器:
String THE_PATTERN = "<a\\s+href\\s*=\\s*\"/([a-zA-Z]+)/([0-9]+)";
Matcher m = Pattern.compile(THE_PATTERN).matcher(THE_INPUT_STRING);
String[] results = new String[2];
if (m.find()) {
results[0] = m.group(1);
results[1] = m.group(2);
}
尚未尝试过,因此可能会出现一些小/易于修复的错误。
关于java - 如何从字符串中获取特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9257608/