← Back to Core Java Course | Chapter 6: Strings | Lesson 5 of 5

Java में Regular Expressions

एक regular expression text ढूंढने के लिए एक pattern है, सिर्फ कुछ shapes में fit होने वाले एक stencil जैसा। Java इसके साथ search, replace और split करने के लिए Pattern और Matcher उपयोग करता है।
Syntax
java
import java.util.regex.*;

Pattern pattern = Pattern.compile("regex");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
    matcher.group();
}

Pattern and Matcher Classes

Java का regex support java.util.regex में रहता है, दो classes पर centered: Pattern reuse के लिए एक regular expression को एक बार compile करता है, और Matcher matches search, find, या check करने के लिए उस compiled pattern को एक specific input string के खिलाफ apply करता है।

उदाहरण: Pattern and Matcher Classes

java
// Import java.util.regex.Pattern so it can be used by its short name
import java.util.regex.Pattern;
// Import java.util.regex.Matcher so it can be used by its short name
import java.util.regex.Matcher;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Pattern pattern = Pattern.compile("cat");
		Matcher matcher = pattern.matcher("The cat sat");
		// Print a line to the console
		System.out.println(matcher.find());
	}
}

Character Classes

Character classes, square brackets में लिखे जैसे [aeiou] या [0-9], आपकी list किए set से किसी भी single character से match करते हैं — वे vowels, digit ranges, या allowed symbols की एक specific list match करने के लिए building block हैं बिना हर alternative explicitly लिखे।

उदाहरण: Character Classes

java
public class Main {
	public static void main(String[] args) {
		System.out.println("hello".matches("[aeiou]+")); // false: not all vowels
		System.out.println("a1".matches("[0-9]")); // false
		System.out.println("5".matches("[0-9]"));
	}
}

Using Quantifiers

Quantifiers repetition control करते हैं: * मतलब zero या ज़्यादा, + मतलब एक या ज़्यादा, ? मतलब optional (zero या एक), और {n,m} आपको exact या bounded repeat count specify करने देता है, जैसे बिल्कुल तीन digits के लिए \d{3}।

उदाहरण: Using Quantifiers

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("123".matches("\\d{3}"));
		// Print a line to the console
		System.out.println("color".matches("colou?r"));
		System.out.println("aaa".matches("a+"));
	}
}

Replacing with Regex

replaceAll() एक regex pattern से match होने वाली हर substring को एक call में एक replacement string से बदलता है, जबकि replaceFirst() पहले match के बाद रुकता है — दोनों cleanup tasks जैसे punctuation हटाना या whitespace normalize करना के लिए convenient हैं।

उदाहरण: Replacing with Regex

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare text and set it to "cat bat rat"
		String text = "cat bat rat";
		// Print a line to the console
		System.out.println(text.replaceAll("at", "og"));
		// Print a line to the console
		System.out.println(text.replaceFirst("at", "og"));
	}
}

Splitting Strings with Regex

split() एक String को pieces के एक array में तोड़ने के लिए delimiter के रूप में एक regex उपयोग करता है, जो एक single fixed character पर split करने से ज़्यादा flexible है — उदाहरण के लिए, \s+ एक या ज़्यादा whitespace characters की किसी भी run पर split होता है, कई spaces को एक single split point में collapse करते हुए।

उदाहरण: Splitting Strings with Regex

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare text and set it to "one   two  three"
		String text = "one   two  three";
		// Declare parts and set it to text.split("//s+")
		String[] parts = text.split("\\s+");
		for (String p : parts) {
			// Print a line to the console
			System.out.println(p);
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Java strings में backslashes escape करना भूल जाना, इसलिए \d को "\\d" लिखना चाहिए।
  2. एक सफल find() या matches() के बिना matcher.group() call करना, जो IllegalStateException throw करता है।
  3. matches() उपयोग करना जब partial matches चाहिए हों, क्योंकि इसे पूरी string से match करना चाहिए।
चैप्टर सारांश
  • Strings कई built-in methods वाले objects हैं, और StringBuilder और StringBuffer efficient modification allow करते हैं।
  • String formatting text output को shape देता है।
  • Regular expressions text patterns को match और process करते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.