Java Regex
Describe a text pattern, then choose the matching operation.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Regular Expressions.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
A regular expression describes a set of text patterns. matches checks the entire input; find searches for a matching region. Keep Java string escaping separate from regex escaping when a pattern contains backslashes.
விரிவான விளக்கங்கள்
Java Regular Expressions (Regex)
Regex என்பது characters-ஓட pattern — strings-ல search, replace, validate பண்றதுக்கு use ஆகுது.
Java-ல java.util.regex package use பண்றோம்:
Pattern— regex pattern compile பண்றதுக்குMatcher— pattern-ஐ string-ல match பண்றதுக்குPatternSyntaxException— invalid pattern-க்கு exception
Email validate பண்ற simple regex example:
import java.util.regex.*;
// Basic find
Pattern p = Pattern.compile("codingtamilan", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("I love CodingTamilan!");
System.out.println(m.find()); // true
// Full-string match
System.out.println("Hello".matches("[A-Z][a-z]+")); // true
// replaceAll — remove non-digits
String cleaned = "Phone: 98-765-43210".replaceAll("\\D", "");
System.out.println(cleaned); // 9876543210Real-World Regex Examples
| Use case | Pattern | Notes |
|---|---|---|
[\w.-]+@[\w.-]+\.[a-zA-Z]{2,} | Basic; use a library for production | |
| Indian phone | [6-9]\d{9} | 10-digit, starts 6–9 |
| Digits only | \d+ | One or more digits |
| Alphanumeric | [a-zA-Z0-9]+ | Letters and digits |
| Date dd/mm/yyyy | (0[1-9]|[12]\d|3[01])/(0[1-9]|1[0-2])/\d{4} | Strict day/month range |
Java-வில் regex match செய்ய எந்த method பயன்படுத்துவோம்?
- String.match()
- String.matches() அல்லது Pattern.compile()
- String.find()
- String.regex()
Reveal answer / விடை
String.matches() அல்லது Pattern.compile()
`str.matches(regex)` whole string match செய்யும். Complex patterns-க்கு `Pattern.compile(regex)` மற்றும் `Matcher` class பயன்படுத்துவோம். Email validation, phone number validation-க்கு regex essential.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(Pattern.matches("[A-Z][0-9]{3}", "A123"));
System.out.println(Pattern.matches("[A-Z][0-9]{3}", "A1234"));
}
}Predict first. Then reveal the expected output / வெளியீடு
true false
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Try a123 and B007. Which inputs satisfy the pattern?
Reveal the answer and reasoning / விடை
a123 fails because the first character is lowercase; B007 matches.
தவிர்க்க வேண்டிய தவறு: Pattern.matches is a whole-input check. It does not mean that any matching substring is enough.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.