package org.commonmark.internal;

import org.commonmark.node.Block;
import org.commonmark.node.ThematicBreak;
import org.commonmark.parser.block.*;

import java.util.regex.Pattern;

public class ThematicBreakParser extends AbstractBlockParser {

    private static Pattern PATTERN = Pattern.compile("^(?:(?:\\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})[ \t]*$");

    private final ThematicBreak block = new ThematicBreak();

    @Override
    public Block getBlock() {
        return block;
    }

    @Override
    public BlockContinue tryContinue(ParserState state) {
        // a horizontal rule can never container > 1 line, so fail to match
        return BlockContinue.none();
    }

    public static class Factory extends AbstractBlockParserFactory {

        @Override
        public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
            if (state.getIndent() >= 4) {
                return BlockStart.none();
            }
            int nextNonSpace = state.getNextNonSpaceIndex();
            CharSequence line = state.getLine();
            if (PATTERN.matcher(line.subSequence(nextNonSpace, line.length())).matches()) {
                return BlockStart.of(new ThematicBreakParser()).atIndex(line.length());
            } else {
                return BlockStart.none();
            }
        }
    }
}

@@ -146,41 +146,8 @@
 *;%0A%0A
-import java.util.regex.Pattern;%0A%0A
 publ
@@ -210,128 +210,8 @@
  %7B%0A%0A
-    private static Pattern PATTERN = Pattern.compile(%22%5E(?:(?:%5C%5C*%5B %5Ct%5D*)%7B3,%7D%7C(?:_%5B %5Ct%5D*)%7B3,%7D%7C(?:-%5B %5Ct%5D*)%7B3,%7D)%5B %5Ct%5D*$%22);%0A%0A
     
@@ -944,79 +944,42 @@
 if (
-PATTERN.matcher(line.subSequence(nextNonSpace, line.length())).matches(
+isThematicBreak(line, nextNonSpace
 )) %7B
@@ -1136,32 +1136,32 @@
 ;%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -1159,11 +1159,1165 @@
  %7D%0A    %7D
+%0A%0A    // spec: A line consisting of 0-3 spaces of indentation, followed by a sequence of three or more matching -, _, or *%0A    // characters, each followed optionally by any number of spaces, forms a thematic break.%0A    private static boolean isThematicBreak(CharSequence line, int index) %7B%0A        int dashes = 0;%0A        int underscores = 0;%0A        int asterisks = 0;%0A        for (int i = index; i %3C line.length(); i++) %7B%0A            switch (line.charAt(i)) %7B%0A                case '-':%0A                    dashes++;%0A                    break;%0A                case '_':%0A                    underscores++;%0A                    break;%0A                case '*':%0A                    asterisks++;%0A                    break;%0A                case ' ':%0A                case '%5Ct':%0A                    // Allowed, even between markers%0A                    break;%0A                default:%0A                    return false;%0A            %7D%0A        %7D%0A%0A        return ((dashes %3E= 3 && underscores == 0 && asterisks == 0) %7C%7C%0A                (underscores %3E= 3 && dashes == 0 && asterisks == 0) %7C%7C%0A                (asterisks %3E= 3 && dashes == 0 && underscores == 0));%0A    %7D
 %0A%7D%0A

Replace regex matching in ThematicBreakParser