ZimZim
ZimZim

Reputation: 3381

Pattern/Matcher in Java?

I have a certain text in Java, and I want to use pattern and matcher to extract something from it. This is my program:

public String getItemsByType(String text, String start, String end) {

    String patternHolder;
    StringBuffer itemLines = new StringBuffer();

    patternHolder = start + ".*" + end;

    Pattern pattern = Pattern.compile(patternHolder);
    Matcher matcher = pattern.matcher(text);

    while (matcher.find()) {
        itemLines.append(text.substring(matcher.start(), matcher.end())
                + "\n");
    }

    return itemLines.toString();
}

This code works fully WHEN the searched text is on the same line, for instance:

String text = "My name is John and I am 18 years Old"; 

getItemsByType(text, "My", "John");

immediately grabs the text "My name is John" out of the text. However, when my text looks like this:

String text = "My name\nis John\nand I'm\n18 years\nold"; 

getItemsByType(text, "My", "John"); 

It doesn't grab anything, since "My" and "John" are on different lines. How do I solve this?

Upvotes: 3

Views: 2140

Answers (2)

Bohemian
Bohemian

Reputation: 425013

Use this instead:

Pattern.compile(patternHolder, Pattern.DOTALL);

From the javadoc, the DOTALL flag means:

Enables dotall mode.

In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.

Upvotes: 7

Michał Kosmulski
Michał Kosmulski

Reputation: 10020

Use Pattern.compile(patternHolder, Pattern.DOTALL) to compile the pattern. This way the dot will match the newline. By default, newline is treated in a special way and not matched by the dot.

Upvotes: 1

Related Questions