package com.todoist.markup;

import java.util.regex.Pattern;

class Patterns {
    private static final String PATTERN_BASE = "%s\\s*((?!%s).+?)\\s*%s";

    public static final Pattern HEADER = Pattern.compile("^\\*\\s+");
    public static final Pattern BOLD = Pattern.compile(PATTERN_BASE.replaceAll("%s", "(?:__|!!|\\\\*\\\\*)"));
    public static final Pattern ITALIC = Pattern.compile(PATTERN_BASE.replaceAll("%s", "[_\\*]"));
    public static final Pattern INLINE_CODE = Pattern.compile(PATTERN_BASE.replaceAll("%s", "`"));
    public static final Pattern CODE_BLOCK = Pattern.compile(PATTERN_BASE.replaceAll("%s", "`{3}"), Pattern.DOTALL);
    public static final Pattern LINK = Pattern.compile("((?:[a-zA-Z]+)://[^\\s]+)(?:\\s+\\(([^)]+)\\))?");
    public static final Pattern GMAIL = Pattern.compile("\\[\\[gmail=\\s*(.*?)\\s*,\\s*(.*?)\\s*\\]\\]");
    public static final Pattern OUTLOOK = Pattern.compile("\\[\\[outlook=\\s*(.*?)\\s*,\\s*(.*?)\\s*\\]\\]");
    public static final Pattern THUNDERBIRD =
            Pattern.compile("\\[\\[thunderbird\\n?\\s*([^\\n]+)\\s+([^\\n]+)\\s+\\]\\]");
}

@@ -76,83 +76,8 @@
 s %7B%0A
-    private static final String PATTERN_BASE = %22%25s%5C%5Cs*((?!%25s).+?)%5C%5Cs*%25s%22;%0A%0A
     
@@ -138,24 +138,72 @@
 %5E%5C%5C*%5C%5Cs+%22);%0A
+    // Bold text is wrapped with __ or !! or **%0A
     public s
@@ -249,39 +249,33 @@
 ile(
-PATTERN_BASE.replaceAll(%22%25s%22, %22
+%22(?:__%7C!!%7C%5C%5C*%5C%5C*)%5C%5Cs*((?!
 (?:_
@@ -285,22 +285,84 @@
 !%7C%5C%5C
+*
 %5C%5C*
-%5C%5C
+)).+?)%5C%5Cs*(?:__%7C!!%7C%5C%5C*
 %5C%5C*)%22)
-)
 ;%0A
+    // Italic text is wrapped with _ or *%0A
     
@@ -418,47 +418,46 @@
 ile(
-PATTERN_BASE.replaceAll(%22%25s%22, %22
+%22%5B_%5C%5C*%5D%5C%5Cs*((?!%5B_%5C%5C*%5D).+?)%5C%5Cs*
 %5B_%5C%5C*%5D%22)
 );%0A 
@@ -452,19 +452,55 @@
 %5B_%5C%5C*%5D%22)
-)
 ;%0A
+    // Inline code is wrapped with %60%0A
     publ
@@ -557,44 +557,70 @@
 ile(
-PATTERN_BASE.replaceAll(%22%25s%22, %22%60%22));
+%22%60%5C%5Cs*((?!%60).+?)%5C%5Cs*%60%22);%0A    // Code block is wrapped with %60%60%60
 %0A   
@@ -681,45 +681,48 @@
 ile(
-PATTERN_BASE.replaceAll(%22%25s%22, %22%60%7B3%7D%22)
+%22(?:%60%60%60)%5C%5Cs*((?!(?:%60%60%60)).+?)%5C%5Cs*(?:%60%60%60)%22
 , Pa

Refactor patterns