简体   繁体   English

从URL正则表达式Java中提取数字

[英]Extract number from an URL regex java

I need to extract an last number from an URL followed by a dash. 我需要从URL中提取最后一个数字,然后加上破折号。

Example: 例:

http://www.example.com/p-test-test1-a-12345.html http://www.example.com/p-test-test1-a-12345.html

i need to extract the 12345 using regex. 我需要使用正则表达式提取12345。

i tried this -\\d(.*?).html which gives me 2345 not sure why it removes 1 any idea? 我试过这个-\\d(.*?).html ,它给我2345不确定为什么要删除1个想法?

It removes the first digit as you have invalid pattern it captures everything after -digit 它会删除第一个数字,因为您具有无效模式,它会捕获-digit之后的所有内容

-\\d(.*?).html

-\\d - matches a dash followed by a digit -\\d 匹配一个破折号和一个数字

(.*?) - captures any character (except new line) 0 or more times till next token is satisifed (.*?) - 捕获任何字符(换行符除外)0次或更多次,直到满足下一个标记

. - matches any character (except new line) - 匹配任何字符(换行符除外)

html - matches html html 匹配 html


Try this pattern: 试试这个模式:

PATTERN 图案

(?<=-)\d+(?=\.html)

You must add \\d to group: -(\\d.*?).html 您必须将\\d添加到组: -(\\d.*?).html

if it must be only digits then -(\\d+)\\.html is better. 如果必须仅是数字,则-(\\d+)\\.html更好。

You're looking for a dash, then a digit, then capturing all characters before ".html", which is why the 1 was not captured. 您要查找破折号,然后是数字,然后捕获“ .html”之前的所有字符,这就是为什么未捕获1的原因。

Try this instead: 尝试以下方法:

-(\\d+)\\.html

Try This : 尝试这个 :

String pattern2 = ".*?(\\d+)\\.html";
System.out.println(s.replaceAll(pattern2, "$1"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM