Java Regular Expressions (Regex) ব্যবহারে কিছু predefined character classes রয়েছে, যেগুলি খুবই সাধারণ এবং ব্যবহার করা সহজ। এগুলি বিশেষ প্যাটার্নের জন্য নির্দিষ্ট অর্থ বহন করে এবং regex লেখা অনেক সহজ করে তোলে। \d, \w, \s, \D, \W, \S এগুলি Java regex এর predefined character classes। এগুলির মধ্যে প্রতিটি একটি নির্দিষ্ট ধরনের ক্যারেক্টার বা স্ট্রিংকে মেলানোর জন্য ব্যবহৃত হয়।
নিচে প্রতিটি predefined character class এবং তাদের ব্যবহার সম্পর্কে বিস্তারিত আলোচনা করা হলো।
1. \d: Match any digit (0-9)
\dএকটি digit এর জন্য regex প্যাটার্ন। এটি 0 থেকে 9 এর মধ্যে যেকোনো একক সংখ্যা মেলে।
ব্যবহার:
\dএমন যে কোনও সংখ্যা মেলাতে ব্যবহার করা হয়, যেমন: phone numbers, zip codes ইত্যাদি।
উদাহরণ:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// Regex pattern to match a digit
String pattern = "\\d";
// String to search
String text = "abc123";
// Compile the pattern
Pattern p = Pattern.compile(pattern);
// Create a matcher to find the pattern in the text
Matcher m = p.matcher(text);
// Find digits in the text
while (m.find()) {
System.out.println("Digit found: " + m.group());
}
}
}
আউটপুট:
Digit found: 1
Digit found: 2
Digit found: 3
2. \w: Match any word character (alphanumeric + underscore)
\wদ্বারা word characters (অলফাবেট, সংখ্যা, এবং আন্ডারস্কোর) মেলে। এটি ইংরেজি অক্ষর (lowercase বা uppercase), সংখ্যা (0-9), এবং আন্ডারস্কোর (_) কে মেলে।
উদাহরণ:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// Regex pattern to match a word character
String pattern = "\\w";
// String to search
String text = "Hello_123";
// Compile the pattern
Pattern p = Pattern.compile(pattern);
// Create a matcher to find the pattern in the text
Matcher m = p.matcher(text);
// Find word characters in the text
while (m.find()) {
System.out.println("Word character found: " + m.group());
}
}
}
আউটপুট:
Word character found: H
Word character found: e
Word character found: l
Word character found: l
Word character found: o
Word character found: _
Word character found: 1
Word character found: 2
Word character found: 3
3. \s: Match any whitespace character (spaces, tabs, line breaks)
\sযেকোনো whitespace character মেলে, যেমন space, tab, line breaks (newlines) ইত্যাদি।
উদাহরণ:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// Regex pattern to match a whitespace character
String pattern = "\\s";
// String to search
String text = "Hello World!";
// Compile the pattern
Pattern p = Pattern.compile(pattern);
// Create a matcher to find the pattern in the text
Matcher m = p.matcher(text);
// Find whitespace characters in the text
while (m.find()) {
System.out.println("Whitespace character found: " + m.group());
}
}
}
আউটপুট:
Whitespace character found:
4. \D: Match any non-digit character
\Dএমন non-digit character এর জন্য ব্যবহৃত হয়। এটি 0 থেকে 9 ছাড়া যেকোনো কিছু মেলে।
উদাহরণ:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// Regex pattern to match non-digit characters
String pattern = "\\D";
// String to search
String text = "abc123";
// Compile the pattern
Pattern p = Pattern.compile(pattern);
// Create a matcher to find the pattern in the text
Matcher m = p.matcher(text);
// Find non-digit characters in the text
while (m.find()) {
System.out.println("Non-digit character found: " + m.group());
}
}
}
আউটপুট:
Non-digit character found: a
Non-digit character found: b
Non-digit character found: c
5. \W: Match any non-word character (anything except alphanumeric + underscore)
\Wএমন non-word characters মেলে, যা\wএর বিপরীত। এটি শুধুমাত্র আলফানিউমেরিক (অক্ষর, সংখ্যা, আন্ডারস্কোর) ছাড়া বাকী সমস্ত ক্যারেক্টার মেলে।
উদাহরণ:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// Regex pattern to match non-word characters
String pattern = "\\W";
// String to search
String text = "Hello@123";
// Compile the pattern
Pattern p = Pattern.compile(pattern);
// Create a matcher to find the pattern in the text
Matcher m = p.matcher(text);
// Find non-word characters in the text
while (m.find()) {
System.out.println("Non-word character found: " + m.group());
}
}
}
আউটপুট:
Non-word character found: @
6. \S: Match any non-whitespace character
\Sহল non-whitespace character এর জন্য। এটি\sএর বিপরীত এবং সমস্ত স্পেস, ট্যাব, এবং নিউলাইন ছাড়া যেকোনো ক্যারেক্টার মেলে।
উদাহরণ:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// Regex pattern to match non-whitespace characters
String pattern = "\\S";
// String to search
String text = "Hello World!";
// Compile the pattern
Pattern p = Pattern.compile(pattern);
// Create a matcher to find the pattern in the text
Matcher m = p.matcher(text);
// Find non-whitespace characters in the text
while (m.find()) {
System.out.println("Non-whitespace character found: " + m.group());
}
}
}
আউটপুট:
Non-whitespace character found: H
Non-whitespace character found: e
Non-whitespace character found: l
Non-whitespace character found: l
Non-whitespace character found: o
Non-whitespace character found: W
Non-whitespace character found: o
Non-whitespace character found: r
Non-whitespace character found: l
Non-whitespace character found: d
Non-whitespace character found: !
Java Regular Expressions (Regex) এর predefined character classes যেমন \d, \w, \s, \D, \W, \S খুবই শক্তিশালী এবং ব্যবহার করা সহজ। এগুলি আপনার Regex প্যাটার্ন লেখার সময় সাধারণ ক্যারেক্টার matching-এর জন্য বিশেষভাবে ব্যবহৃত হয়। এগুলির মাধ্যমে আপনি দ্রুত এবং কার্যকরভাবে স্ট্রিং validation, টেক্সট ম্যানিপুলেশন, এবং ডেটা প্রক্রিয়াকরণ করতে পারেন। Java Regex-এ এই character classes ব্যবহার করলে কোড আরও পরিষ্কার এবং সহজ হয়।
Read more