i need to extract the number part from the string which looks like "(7 r 8digits).txt". i need to store this found string in some variable, but i am confused wih the groups twhether o get the string as group() or group(1).

Pattern p = Pattern.compile("\b[0-9]{7,8}\b");
Matcher matcher = p.matcher("num"); //where num looks like10986745.txt r 0986745.txt
String identifier = matcher.group();
System.out.println(identifier);

But i am getting the output as no match found.
So it would be very helpful if some one would clear this

provided the example you gave is normal and not just to illustrate the problem, couldnt you simply use:

String a = "1234567.txt";
Pattern b= Pattern.compile("\\.");
System.out.println(b.split(a)[0]);

Thank you very much for the reply. It worked but im curious to know how can we do this with grouping.....so that i can handle complex regular expressions ....

Use

Pattern.compile("(\\d{7,8}).txt");

and retrieve it with group(1).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.