R
Rishtaara
Java Fundamentals
Lesson 33 of 36Article18 min

RegEx & Threads

Regular expressions match text patterns — emails, phone numbers, passwords.

RegEx — pattern matching

Regular expressions match text patterns — emails, phone numbers, passwords.

Pattern and Matcher classes: Pattern.compile("\\d+").matcher(text).find().

String matches(), replaceAll(), split() also use regex.

Real-life example: RegEx is a bouncer with a rule — "only names starting with A" — checks each guest automatically.

Regex basics
String email = "asha@mail.com";
boolean valid = email.matches("[\\w.-]+@[\\w.-]+\\.\\w+");

String text = "Order 123 done";
String digits = text.replaceAll("[^0-9]", "");
System.out.println(digits); // 123

Threads — parallel execution

Thread class or Runnable interface runs code in parallel. thread.start() begins execution.

Main thread and worker threads run concurrently — watch shared data (synchronization later).

Real-life example: Threads are multiple chefs in one kitchen — each cooks a dish at the same time, faster total service.

Thread with Runnable
class Worker implements Runnable {
    public void run() {
        System.out.println("Worker: " + Thread.currentThread().getName());
    }
}

Thread t = new Thread(new Worker());
t.start();
System.out.println("Main continues...");