i want to search for MAN-xxxx-xxxxx
in a text file using regular expression.Can anyone help me this??
Asked
Active
Viewed 632 times
-6

Tim Biegeleisen
- 502,043
- 27
- 286
- 360

Vikas
- 141
- 1
- 1
- 11
-
What do the `x`'s represent? numbers? letters? What have you tried? – Tim Biegeleisen Nov 24 '16 at 09:04
-
try `MAN-(\w{4})-(\w{5})` – Jens Nov 24 '16 at 09:05
-
@Tim Biegeleisen x is a number – Vikas Nov 24 '16 at 09:06
-
Did you tried to write a Regex ? DId you tried to match the String to this Regex ? Provide something or produce it before asking. – AxelH Nov 24 '16 at 09:10
-
MAN-(\w{4})-(\w{5}) .it is showing this error:Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) .@Jens – Vikas Nov 24 '16 at 09:10
-
1I gave you a working solution below, did you bother to have a look at it? – Tim Biegeleisen Nov 24 '16 at 09:11
-
Because in Java you need to escape `\\` in a String. – AxelH Nov 24 '16 at 09:11
-
Possible duplicate of [Regex Search In Files using Java](http://stackoverflow.com/questions/9169379/regex-search-in-files-using-java) – DimaSan Nov 24 '16 at 09:20
-
Sorry @Tim Biegeleisen. The solution that you gave worked fine for me.Thanks a lot – Vikas Nov 24 '16 at 09:25
1 Answers
1
please find below working code
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String line = "This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK?This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK?";
String pattern = "(MAN-\\d{4}-\\d{5})"; // The 2 and 3 match only for digit
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
//first Occurrence
/*if (m.find( )) {
System.out.println("Found value: " + m.group() );
}else {
System.out.println("NO MATCH");
}*/
//For all the occurrence
while(m.find( )) {
System.out.println("Found value: " + m.group(0));
}
}
}

mohan rathour
- 420
- 2
- 12
-
wow it works.Even i tried the same code but it was giving the whole line from the text file.I think there was an issue with the pattern.But now in your program it works fine.Thank you – Vikas Nov 24 '16 at 10:07
-
I have not handled that case, you can handle it in if condition. – mohan rathour Nov 24 '16 at 10:19
-
if there are two occurrences of the pattern the above program wont print both the occurrences – Vikas Nov 24 '16 at 10:36
-
no, In this case you can get via m.group[1] ... something like that. – mohan rathour Nov 24 '16 at 10:45
-
-
If i know there are only two occurrences then i can use m.group(1). If the string is a text file then we won't know how many occurrences will be available.In general how can we write it?? – Vikas Nov 24 '16 at 10:50
-
then you can check the length of group, can you please accept the answer – mohan rathour Nov 24 '16 at 10:52
-
-
-
Please find the updated code you will get all the occurrences it this case. – mohan rathour Nov 24 '16 at 11:29