Java में Regular Expressions
In this page:
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
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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]"));
}
}
Login to try C/C++/Java/PHP code in the editor
Using Quantifiers
Quantifiers repetition control करते हैं: * मतलब zero या ज़्यादा, + मतलब एक या ज़्यादा, ? मतलब optional (zero या एक), और {n,m} आपको exact या bounded repeat count specify करने देता है, जैसे बिल्कुल तीन digits के लिए \d{3}।
उदाहरण: Using Quantifiers
// 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+"));
}
}
Login to try C/C++/Java/PHP code in the editor
Replacing with Regex
replaceAll() एक regex pattern से match होने वाली हर substring को एक call में एक replacement string से बदलता है, जबकि replaceFirst() पहले match के बाद रुकता है — दोनों cleanup tasks जैसे punctuation हटाना या whitespace normalize करना के लिए convenient हैं।
उदाहरण: Replacing with Regex
// 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"));
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Java strings में backslashes escape करना भूल जाना, इसलिए
\dको"\\d"लिखना चाहिए। - एक सफल
find()याmatches()के बिनाmatcher.group()call करना, जोIllegalStateExceptionthrow करता है। matches()उपयोग करना जब partial matches चाहिए हों, क्योंकि इसे पूरी string से match करना चाहिए।
- Strings कई built-in methods वाले objects हैं, और
StringBuilderऔरStringBufferefficient 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: