[英]Java regular expression match two same number
I want to use RE to match the file paths like below: 我想使用RE来匹配如下所示的文件路径:
../90804/90804_0.jpg
../89246/89246_8.jpg
../89247/89247_14.jpg
Currently, I use the code as below to match: 目前,我使用以下代码进行匹配:
Pattern r = Pattern.compile("^(.*?)[/](\\d+?)[/](\\d+?)[_](\\d+?).jpg$");
Matcher m = r.matcher(file_path);
But I found it will be an unexpected match like for: 但我发现这将是意外的匹配,例如:
../90804/89246_0.jpg
Is impossible in RE to match two same number? RE中是否不可能匹配两个相同的数字?
You can use this regex with a capture group and back-reference of the same: 您可以将此正则表达式与捕获组和相同的反向引用一起使用:
(\d+)/\1
Equivalent Java regex string will be: 等效的Java正则表达式字符串为:
final String regex = "(\\d+)/\\1";
Details: 细节:
(\\d+)
: Match 1+ digits and capture it in group #1 (\\d+)
:匹配1+个数字并将其捕获到组#1中 /
: Math literal /
/
:数学文字/
\\1
: Using back-reference #1, match same number as in group #1 \\1
:使用后向引用#1,匹配与组#1相同的数字 You may use a \\2
backreference instead of the second \\d+
here: 您可以在此处使用\\2
反向引用而不是第二个\\d+
:
s.matches("(.*?)/(\\d+)/(\\2)_(\\d+)\\.jpg")
See the regex demo . 参见regex演示 。 Note that if you use matches
method, you won't need ^
and $
anchors. 请注意,如果使用matches
方法,则不需要^
和$
锚。
Details 细节
(.*?)
- Group 1: any 0+ chars other than line break chars as few as possible (.*?)
-组1:除换行符以外的任何0+个字符都尽可能少 /
- a slash /
-斜线 (\\\\d+)
- Group 2: one or more digits (\\\\d+)
-第2组:一个或多个数字 /
- a slash /
-斜线 (\\\\2)
- Group 3: the same value as in Group 2 (\\\\2)
-第3组:与第2组相同的值 _
- an underscore _
下划线 (\\\\d+)
- Group 4: one or more digits (\\\\d+)
-第4组:一位或多位数字 \\\\.jpg
- .jpg
. \\\\.jpg
- .jpg
。 Pattern r = Pattern.compile("(.*?)/(\\d+)/(\\2)_(\\d+)\\.jpg");
Matcher m = r.matcher(file_path);
if (m.matches()) {
System.out.println("Match found");
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
System.out.println(m.group(4));
}
Output: 输出:
Match found
..
90804
90804
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.