Vikas Vikas - 8 days ago 7
Java Question

i want to search for "MAN-xxxx-xxxxx" in a text file using regular expression.Can anyone help me this?

i want to search for "MAN-xxxx-xxxxx" in a text file using regular expression.Can anyone help me this??

Answer

You can try the following:

String line = "some line MAN-1234-12345 from your file";

if (line.matches(".*MAN-\\d{4}-\\d{5}.*") {
    System.out.println("the line matches");
    // or however you want to handle the line
}

If you also need to capture the regex pattern for some reason, you can use a matcher:

String line = "some line MAN-1234-12345 from your file";
String pattern = "(MAN-\\d{4}-\\d{5})";
Pattern pattern = Pattern.compile(pattern);
Matcher matcher = pattern.matcher(line);

if (matcher.find()) {
    System.out.println("found: " + matcher.group(1));
}
Comments