How can I get the the second matcher in regex in Java? [duplicate]
Clash Royale CLAN TAG#URR8PPP
How can I get the the second matcher in regex in Java? [duplicate]
This question already has an answer here:
I want to extract the second matcher in a regex pattern between -
and _
in this string:
-
_
VA-123456-124_VRG.tif
I tried this:
Pattern mpattern = Pattern.compile("-.*?_");
But I get 123456-124
for the above regex in Java.
123456-124
I need only 124
.
124
How can I achieve this?
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
6 Answers
6
I would use a formal pattern matcher here, to be a specific as possible. I would use this pattern:
^[^-]+-[^-]+-([^_]+).*
and then check the first capture group for the possible match. Here is a working code snippet:
String input = "A-123456-124_VRG.tif";
String pattern = "^[^-]+-[^-]+-([^_]+).*";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
if (m.find())
System.out.println("Found value: " + m.group(1) );
124
Demo
By the way, there is a one liner which would also work here:
System.out.println(input.split("[_-]")[2]);
But, the caveat here is that it is not very specific, and might fail for your other data.
If you know that's your format, this will return the requested digits.
Everything before the underscore that is not a dash
Pattern pattern = Pattern.compile("([^-]+)_");
You know you want only digits so be more specific Pattern.compile("-([0-9]+)_");
Pattern.compile("-([0-9]+)_");
or simply
-(d+)_
– Reimeus
Aug 8 at 11:17
-(d+)_
@Reimeus thought about that but d needs the escaped in a string to get it to compile and I come from the Perl world where d matches all Unicode decimal digits not just [0-9] so I was being explicit in what to match
– JGNI
Aug 8 at 11:27
Try using below regex:
.*-(.*?)_
What this will do is : .* will match all the characters till it finds - . Also, as it is greedy, it will try to find the last possible option, which is just before 24
Demo: https://regex101.com/r/NWgZoH/1
JShell Output:
jshell> Pattern pattern = Pattern.compile(".*-(.*?)_");
pattern ==> .*-(.*?)_
jshell> Matcher matcher = pattern.matcher("VA-123456-124_VRG.tif");
matcher ==> java.util.regex.Matcher[pattern=.*-(.*?)_ region=0,21 lastmatch=]
jshell> if(matcher.find())
...> System.out.println(matcher.group(1));
...>
124
Your test case are very low, but if I answer your test case I think below regex can be helpful.
-.*-(.*)_
then extract first group.
if you just want to extract in simple way go ahead with this,
public static void main(String args)
String s = "VA-123456-124_VRG.tif";
System.out.println(s.split("[_-]")[2]);
This is not really a 'duplicate'. Although the @sAaCh requests the second match, he wants the match before a marker and his approach was not restrictive enough. Although another solution solves the problem, there is an easier solution to his issue that involves a much simpler approach.
– Steven Spungin
Aug 8 at 14:18