Regular expression question - non-capturing groups
Hello.
I want to be able to extract a four digit number from a piece of text, which may or may not have another three-digit code preceding it.
For example, if the string was:
HHHH_9017564_UUP.txt
I would want to be able to extract "7564". "901" may or may not be there, so if I just searched for a four-digit number I would get "9017" some of the time.
I want to be able to check for the presence of "901", without returning it as part of the capture. How do I do this in a single regular expression?
Here is my code:
public static void main(String[] args)
{
String sInput = args[0];
String sRegex = "(?:901)?[0-9]{4}";
Pattern p = Pattern.compile(sRegex);
Matcher m = p.matcher(sInput);
System.out.println(m.matches());
while (m.find())
{
System.out.println(sInput.substring(m.start(), m.end()));
}
System.out.println("No more matches to find.");
}
I though (?:X) was a non-capturing group, but maybe this doesn't do what I though it did?
Chris Jarvis
|