Regex Flags Multiline Case Insensitive Dotall

MULTILINE re-anchors ^ and $ per line, CASE_INSENSITIVE folds letter case, DOTALL lets dot cross newlines; flags combine with bitwise OR.

JavaData Integrity

Code

snippet
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogPatternScanner {

    public static void main(String[] args) {
        String log = "WARN disk almost full\nINFO deploy ok\nWARN cache thrashing";

        // 无 MULTILINE:^ 只锚定整段输入的开头
        System.out.println("plain ^WARN hits:      "
                + count(Pattern.compile("^WARN").matcher(log)));

        // MULTILINE:^ / $ 改为按行锚定,两行 WARN 都命中
        System.out.println("multiline ^WARN hits:  "
                + count(Pattern.compile("^WARN", Pattern.MULTILINE).matcher(log)));

        // CASE_INSENSITIVE:大小写折叠;默认只覆盖 ASCII,非 ASCII 需叠加 UNICODE_CASE
        System.out.println("insensitive warn hits: "
                + count(Pattern.compile("warn", Pattern.CASE_INSENSITIVE).matcher(log)));

        // DOTALL:让点号吞掉换行,跨行匹配
        System.out.println("dotall full...ok:      "
                + Pattern.compile("full.*ok", Pattern.DOTALL).matcher(log).find());

        // 多个 flag 用位或组合
        System.out.println("combo per-line:        "
                + Pattern.compile("^warn.*full$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE)
                        .matcher(log).find());
    }

    private static int count(Matcher matcher) {
        int hits = 0;
        while (matcher.find()) {
            hits++;
        }
        return hits;
    }
}

What the code does

Regex Flags Multiline Case Insensitive Dotall is built around the class LogPatternScanner and the functions main and count.

The cycling part of this snippet is while (matcher.find()).

Applying it

The demo output is labeled (plain ^WARN hits:, multiline ^WARN hits:, insensitive warn hits:), so every printed line maps back to its originating operation.

The entry point wires count together — run it once and the whole demo flows.

Statements that matter

+ Pattern.compile("^warn.*full$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE)
.matcher(log).find());
String log = "WARN disk almost full\nINFO deploy ok\nWARN cache thrashing";
int hits = 0;

Interactive Tools for This Topic

FAQ

What does Regex Flags Multiline Case Insensitive Dotall print when run?
Run it as a complete program — the output lines carry labels such as `plain ^WARN hits:`, `multiline ^WARN hits:`, `insensitive warn hits:`.