ItTechGenie-Subject - Java Regex (Warm-up + 10 Questions)

Java Question Bank

Java Regex | Q0 Warm‑up (Show/Hide Answer) + Q1–Q10 Scenario Questions (5 Moderate + 5 Complex) — With Answers

Instruction: Boilerplate is visible by default. Students implement ONLY methods marked // ✅ TODO.
Answers: Available via Show/Hide Answer.
Warm‑up: 1 (Q0) Moderate: 5 (Q1–Q5) Complex: 5 (Q6–Q10) Inputs: spaces + quotes + unicode α/β + emoji ✅ + special chars
0

Warm‑up: Extract orderId + amount using capturing groups (with answer)

PatternMatchergroupsWarm-up

Warm‑up: Input is a single log line: "Order[ORD-α1✅] paid ₹1200.50 by user='Gopi Suresh'" Task: 1) Use a regex with capturing groups to extract: - orderId (inside Order[...]) - amount (number with optional decimal) 2) Print both values. Implement ONLY: warmUp()

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.regex.*;

public class Main {

    // ✅ TODO: Student must implement only this method
    static void warmUp() {
        // TODO:
        // - define log line
        // - build Pattern with groups: Order\[(...)] and amount
        // - use Matcher.find()
        // - print orderId and amount
        throw new UnsupportedOperationException("TODO");
    }

    public static void main(String[] args) {
        warmUp();
    }
}

Answer (Logic + Code)

Logic: - Pattern compiles the regex. - Matcher scans the input. - Parentheses (...) create capturing groups accessible via group(1), group(2).

static void warmUp() {
    String log = "Order[ORD-α1✅] paid ₹1200.50 by user='Gopi Suresh'";

    Pattern p = Pattern.compile("Order\[(.+?)]\s+paid\s+[^0-9]*([0-9]+(?:\.[0-9]+)?)");
    Matcher m = p.matcher(log);

    if (m.find()) {
        String orderId = m.group(1);   // group1 = inside Order[...]
        String amount  = m.group(2);   // group2 = numeric amount
        System.out.println(orderId);
        System.out.println(amount);
    } else {
        System.out.println("No match");
    }
}
1

Validate username — letters/digits/underscore + unicode α/β allowed

matchescharacter classesanchorsModerate

Scenario: Registration requires username rules: - 3 to 20 chars - allowed: letters, digits, underscore - additionally allow greek α and β - no spaces Return true/false. Implement ONLY: UserRules.isValid(username) Print result for multiple test usernames.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;
import java.util.regex.*;

class UserRules {
    // ✅ TODO: Student must implement only this method
    static boolean isValid(String username) {
        // TODO:
        // - build regex with anchors ^ $
        // - length 3..20
        // - allowed set: [A-Za-z0-9_αβ]
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        List<String> u = Arrays.asList("gopi_suresh", "go", "Gopi✅", "αβ_user", "user name", "__ok__");
        for (String x : u) {
            System.out.println(x + " => " + UserRules.isValid(x));
        }
    }
}

Answer (Logic + Code)

Use ^[A-Za-z0-9_αβ]{3,20}$ and Pattern.matches or string.matches().

static boolean isValid(String username) {
    if (username == null) return false;
    String s = username.trim();
    return s.matches("^[A-Za-z0-9_αβ]{3,20}$");
}
2

Extract all emails from text — find() in loop

findMatcher loopgroupsModerate

Scenario: Support chat contains multiple emails mixed with text and emojis. Task: - Extract all email addresses into a List<String> - Preserve order - Trim around punctuation if needed Implement ONLY: EmailExtractor.extract(text) Print list.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;
import java.util.regex.*;

class EmailExtractor {
    // ✅ TODO: Student must implement only this method
    static List<String> extract(String text) {
        // TODO:
        // - create Pattern
        // - loop matcher.find()
        // - add matcher.group()
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        String text = "Mail gopi@ittechgenie.com, cc: suresh✅@mail.com; alt: α/β@x.io !!!";
        System.out.println(EmailExtractor.extract(text));
    }
}

Answer (Logic + Code)

Pattern like [^\s,;]+@[^\s,;]+\.[^\s,;]+ then find() repeatedly.

static List<String> extract(String text) {
    List<String> out = new ArrayList<>();
    if (text == null) return out;

    Pattern p = Pattern.compile("[^\s,;]+@[^\s,;]+\.[^\s,;]+");
    Matcher m = p.matcher(text);

    while (m.find()) {
        out.add(m.group());
    }
    return out;
}
3

Normalize whitespace — replaceAll(\s+) + trim

replaceAll\s+trimModerate

Scenario: Imported addresses contain random tabs/newlines and extra spaces. Task: - Replace any whitespace run (spaces/tabs/newlines) with one space - Trim edges Implement ONLY: TextNorm.clean(s) Print cleaned string.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
class TextNorm {
    // ✅ TODO: Student must implement only this method
    static String clean(String s) {
        // TODO:
        // - replaceAll("\\s+"," ")
        // - trim
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(TextNorm.clean("  12\tMG  Road\nBengaluru   ✅   α/β   "));
    }
}

Answer (Logic + Code)

Use s.replaceAll("\\s+"," ").trim();

static String clean(String s) {
    if (s == null) return "";
    return s.replaceAll("\\s+", " ").trim();
}
4

Validate strong password — lookaheads

lookaheadsecuritymatchesModerate

Scenario: Password policy: - min 8 chars - at least 1 uppercase - at least 1 lowercase - at least 1 digit - at least 1 special char from: !@#$ - no spaces Return true/false. Implement ONLY: PasswordRules.isStrong(pwd)

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;

class PasswordRules {
    // ✅ TODO: Student must implement only this method
    static boolean isStrong(String pwd) {
        // TODO:
        // - use regex lookaheads:
        //   (?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$])\S{8,}
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        List<String> p = Arrays.asList("Abc123!@", "abc123!@", "ABC123!@", "Abcdefgh", "Abc 123!@", "A1b2c3d4$✅");
        for (String x : p) System.out.println(x + " => " + PasswordRules.isStrong(x));
    }
}

Answer (Logic + Code)

Use lookaheads to assert presence without consuming characters; \S ensures no spaces.

static boolean isStrong(String pwd) {
    if (pwd == null) return false;
    return pwd.matches("^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[!@#$])\\S{8,}$");
}
5

Parse key=value pairs — quoted values with spaces

groupsnon-greedyalternationModerate

Scenario: Command line: PAY amount="1200" currency=INR note="alpha β ✅" ref=TXN-001!@# Task: Extract all pairs into Map<String,String>. Values can be: - "quoted with spaces" - or plain without spaces Implement ONLY: KVParser.parse(cmd) Print map.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;
import java.util.regex.*;

class KVParser {
    // ✅ TODO: Student must implement only this method
    static Map<String,String> parse(String cmd) {
        // TODO:
        // - Pattern: (\w+)=(".*?"|\S+)
        // - remove quotes if present
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        String cmd = "PAY amount=\"1200\" currency=INR note=\"alpha β ✅\" ref=TXN-001!@#";
        System.out.println(KVParser.parse(cmd));
    }
}

Answer (Logic + Code)

Use Matcher.find() to capture key group(1) and value group(2); strip outer quotes.

static Map<String,String> parse(String cmd) {
    Map<String,String> out = new LinkedHashMap<>();
    if (cmd == null) return out;

    Pattern p = Pattern.compile("(\\w+)=(\".*?\"|\S+)");
    Matcher m = p.matcher(cmd);

    while (m.find()) {
        String key = m.group(1);
        String val = m.group(2);
        if (val.startsWith(""") && val.endsWith(""") && val.length() >= 2) {
            val = val.substring(1, val.length() - 1);
        }
        out.put(key, val);
    }
    return out;
}
6

Validate IPv4 address — regex + range check (0–255)

regex + logicgroupsvalidationComplex

Scenario: Network config accepts IPv4 only. Task: - Validate format: ddd.ddd.ddd.ddd (1-3 digits each) - Then ensure each octet is between 0 and 255 Return true/false. Implement ONLY: NetRules.isValidIPv4(ip) Print results for a list.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;
import java.util.regex.*;

class NetRules {
    // ✅ TODO: Student must implement only this method
    static boolean isValidIPv4(String ip) {
        // TODO:
        // - regex with 4 groups: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})
        // - parse each group as int and check 0..255
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        List<String> ips = Arrays.asList("192.168.0.1", "256.1.1.1", "1.2.3", "01.02.03.004", "10.0.0.✅", "0.0.0.0");
        for (String x : ips) System.out.println(x + " => " + NetRules.isValidIPv4(x));
    }
}

Answer (Logic + Code)

Regex checks shape; then integer range checks ensure correctness.

static boolean isValidIPv4(String ip) {
    if (ip == null) return false;

    Pattern p = Pattern.compile("^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$");
    Matcher m = p.matcher(ip.trim());
    if (!m.matches()) return false;

    for (int i = 1; i <= 4; i++) {
        int v = Integer.parseInt(m.group(i));
        if (v < 0 || v > 255) return false;
    }
    return true;
}
7

Redact phone numbers — replaceAll with groups

replaceAllgroupsPIIComplex

Scenario: Logs may contain phone numbers: - formats: 10 digits, or +91-XXXXXXXXXX, or (080)XXXXXXX (ignore) Task: - Mask 10-digit mobile numbers by keeping last 2 digits: 98******12 - Should work inside larger text Implement ONLY: PiiRedactor.maskPhones(text) Print masked text.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.regex.*;

class PiiRedactor {
    // ✅ TODO: Student must implement only this method
    static String maskPhones(String text) {
        // TODO:
        // - normalize separators (optional)
        // - regex to capture 10 digits possibly with +91- prefix
        // - replace using groups: first2 + ****** + last2
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        String text = "Call +91-9876543212 or 9876543212. Backup: '98765 43212' ✅";
        System.out.println(PiiRedactor.maskPhones(text));
    }
}

Answer (Logic + Code)

Use a regex that captures 10 digits even if split by space; use replaceAll with backreferences.

static String maskPhones(String text) {
    if (text == null) return "";

    // normalize common split format: 98765 43212 -> 9876543212
    String s = text.replaceAll("(\d{5})\s+(\d{5})", "$1$2");

    // capture optional +91- then 10 digits; capture first2 and last2
    Pattern p = Pattern.compile("(?:\+91-)?(\d{2})\d{6}(\d{2})");
    Matcher m = p.matcher(s);

    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String rep = m.group(1) + "******" + m.group(2);
        m.appendReplacement(sb, rep);
    }
    m.appendTail(sb);
    return sb.toString();
}
8

Split CSV line respecting quoted commas — Matcher find groups

CSVquoted fieldsMatcherComplex

Scenario: CSV line: P-1,"Phone, 6.5\" ✅",2,"Bengaluru, KA" Task: Split into fields where commas inside quotes do NOT split. Return List<String> fields (quotes removed, inner quotes unescaped). Implement ONLY: CsvSmart.split(line) Print list.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;
import java.util.regex.*;

class CsvSmart {
    // ✅ TODO: Student must implement only this method
    static List<String> split(String line) {
        // TODO:
        // - regex that matches either quoted field or unquoted token:
        //   "((?:[^"]|"")*)"|([^,]+)
        // - iterate Matcher.find()
        // - unescape "" -> "
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        String line = "P-1,\"Phone, 6.5\\\" ✅\",2,\"Bengaluru, KA\"";
        System.out.println(CsvSmart.split(line));
    }
}

Answer (Logic + Code)

Use a token regex that alternates quoted and unquoted fields; process groups.

static List<String> split(String line) {
    List<String> out = new ArrayList<>();
    if (line == null) return out;

    // Match: quoted field (group1) OR unquoted field (group2) OR empty
    Pattern p = Pattern.compile("\\G(?:\"((?:[^\"]|\\\")*)\"|([^,]*))(?:,|$)");
    Matcher m = p.matcher(line);

    while (m.find()) {
        String q = m.group(1);
        String u = m.group(2);

        String val = (q != null) ? q : (u != null ? u : "");
        // unescape " -> "
        val = val.replace("\\"", """);
        out.add(val);
    }
    return out;
}
9

Parse dates in multiple formats — alternation + named groups style

alternationgroupsnormalizationComplex

Scenario: Invoice dates arrive in multiple formats: - 2026-02-18 - 18/02/2026 - Feb 18, 2026 Task: Convert any supported date into ISO: yyyy-MM-dd. Return empty string if unsupported. Implement ONLY: DateNorm.toIso(text) Print conversions.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;
import java.util.regex.*;

class DateNorm {
    // ✅ TODO: Student must implement only this method
    static String toIso(String text) {
        // TODO:
        // - use 3 patterns or one alternation
        // - extract day/month/year
        // - map month name to number
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        List<String> d = Arrays.asList("2026-02-18", "18/02/2026", "Feb 18, 2026", "18-Feb-2026✅");
        for (String x : d) System.out.println(x + " => " + DateNorm.toIso(x));
    }
}

Answer (Logic + Code)

Try patterns in order; for month names build a lookup map; format using String.format.

static String toIso(String text) {
    if (text == null) return "";
    String s = text.trim();

    // 1) yyyy-MM-dd
    Matcher m1 = Pattern.compile("^(\d{4})-(\d{2})-(\d{2})$").matcher(s);
    if (m1.matches()) return m1.group(1) + "-" + m1.group(2) + "-" + m1.group(3);

    // 2) dd/MM/yyyy
    Matcher m2 = Pattern.compile("^(\d{2})/(\d{2})/(\d{4})$").matcher(s);
    if (m2.matches()) return m2.group(3) + "-" + m2.group(2) + "-" + m2.group(1);

    // 3) Mon dd, yyyy  (e.g., Feb 18, 2026)
    Matcher m3 = Pattern.compile("^([A-Za-z]{3})\s+(\d{1,2}),\s*(\d{4})$").matcher(s);
    if (m3.matches()) {
        String mon = m3.group(1).toLowerCase();
        Map<String,String> mm = new HashMap<>();
        mm.put("jan","01"); mm.put("feb","02"); mm.put("mar","03"); mm.put("apr","04");
        mm.put("may","05"); mm.put("jun","06"); mm.put("jul","07"); mm.put("aug","08");
        mm.put("sep","09"); mm.put("oct","10"); mm.put("nov","11"); mm.put("dec","12");
        String month = mm.get(mon);
        if (month == null) return "";
        String day = String.format("%02d", Integer.parseInt(m3.group(2)));
        return m3.group(3) + "-" + month + "-" + day;
    }

    return "";
}
10

Content moderation — block forbidden words using word boundaries

\b boundariesCASE_INSENSITIVEsanitizeComplex

Scenario: Chat moderation must block messages containing forbidden words: ["refund", "scam", "hack"] Rules: - case-insensitive - whole-word only ("refunded" should NOT match) - message may contain unicode α/β and emoji ✅ Return true if message is allowed (no forbidden words), else false. Implement ONLY: Moderation.isAllowed(msg) Print results.

✅ Includes spaces + quotes + unicode α/β + emoji ✅ + special chars.
Java Regex scenario (students implement ONLY // ✅ TODO)Ctrl+C
import java.util.*;
import java.util.regex.*;

class Moderation {
    // ✅ TODO: Student must implement only this method
    static boolean isAllowed(String msg) {
        // TODO:
        // - build one regex like \b(refund|scam|hack)\b with CASE_INSENSITIVE
        // - return !matcher.find()
        throw new UnsupportedOperationException("TODO");
    }
}

public class Main {
    public static void main(String[] args) {
        List<String> msgs = Arrays.asList(
            "Need a REFUND ✅ now!",
            "This is refunded already",
            "No scam here α/β",
            "Safe message"
        );
        for (String x : msgs) System.out.println(x + " => allowed=" + Moderation.isAllowed(x));
    }
}

Answer (Logic + Code)

Word boundaries ensure whole words only; matcher.find() detects presence anywhere.

static boolean isAllowed(String msg) {
    if (msg == null) return true;
    Pattern p = Pattern.compile("\\b(refund|scam|hack)\\b", Pattern.CASE_INSENSITIVE);
    return !p.matcher(msg).find();
}