我需要从字符串中提取data-user_id
数字
输入字符串示例
data-user_id="987654" lorem epsem lorem epsem lorem epsem lorem
data-user_id="123456-6" lorem epsem epsem lorem epsem
<img src="abcd.com"/> lorem epsem data-user_id="123456"
预期输出
987654,123456-6,123456
我的代码(不起作用)
private static String getIdFromLine(String inputLine) {
Pattern p = Pattern.compile("(data-user_id=\"[0-9a-z]*\")");
Matcher m = p.matcher(inputLine);
if (m.find()) {
String src = m.group(2);
}
return null;
}
最佳答案
你应该有这个正则表达式:
data-user_id=\"([0-9a-z-]+)\"
group(1)
将包含所需的输出。
您的代码存在更多问题;您不对结果进行循环,而不是if
,您应该使用while
循环:
while (m.find()) {
//build the result here
{
关于java - 从字符串中提取自定义 HTML 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28047225/