我应该像这样解析一个字符串:
casale-monferrato/incomincia-oggi-roma-l-ultimo-atto-processo-eternit-davanti-corte-cassazione-74506.html
我想要保存字符串末尾的数字,"-" and ".html"
之间的数字(在这种情况下为74506
)。
解析必须是唯一被认为是最后一个数字的条件,而不应考虑其他数字。
我怎样才能在java中这样做?
我必须使用的正确regexp
是什么?
答案 0 :(得分:3)
使用以下正则表达式仅匹配最后一个数字。
"(?<!\\d)\\d+(?=\\D*$)"
String s = "casale-monferrato/incomincia-oggi-roma-l-ultimo-atto-processo-eternit-davanti-corte-cassazione-74506.html";
Pattern regex = Pattern.compile("(?<!\\d)\\d+(?=\\D*$)");
Matcher matcher = regex.matcher(s);
while(matcher.find()){
System.out.println(matcher.group(0));
}
输出:
74506
正则表达式:
(?<! look behind to see if there is not:
\d digits (0-9)
) end of look-behind
\d+ digits (0-9) (1 or more times)
(?= look ahead to see if there is:
\D* non-digits (all but 0-9) (0 or more
times)
$ before an optional \n, and the end of
the string
) end of look-ahead
答案 1 :(得分:0)
答案 2 :(得分:0)
不需要正则表达式。试试这个:
public static void main(final String[] args) {
final String url =
"casale-monferrato/incomincia-oggi-roma-l-ultimo-"
+ "atto-processo-eternit-davanti-corte-cassazione-74506.html";
final String n = url.substring(url.lastIndexOf('-') + 1, url.lastIndexOf('.'));
System.out.println(n);
}