সাধারণ অক্ষর শ্রেণী “[ ] ” এর সমস্ত নির্দিষ্ট অক্ষরের সাথে মেলে। মেটা অক্ষর ^ উপরের অক্ষর শ্রেণীর মধ্যে নেতিবাচক হিসাবে কাজ করে যেমন নিচের অভিব্যক্তিটি b ছাড়া সমস্ত অক্ষরের সাথে মেলে (স্পেস এবং বিশেষ অক্ষর সহ)
"[^b]"
একইভাবে, নিম্নলিখিত অভিব্যক্তিটি প্রদত্ত ইনপুট স্ট্রিং-এর সমস্ত ব্যঞ্জনবর্ণের সাথে মেলে।
"([^aeiouyAEIOUY0-9\\W]+)";
তারপর আপনি মিলিত অক্ষরগুলিকে রিপ্লেসঅ্যাল() পদ্ধতি ব্যবহার করে খালি স্ট্রিং “” দিয়ে প্রতিস্থাপন করে অপসারণ করতে পারেন।
উদাহরণ 1
public class RemovingConstants {
public static void main( String args[] ) {
String input = "Hi welc#ome to t$utori$alspoint";
String regex = "([^aeiouAEIOU0-9\\W]+)";
String result = input.replaceAll(regex, "");
System.out.println("Result: "+result);
}
} আউটপুট
Result: i e#oe o $uoi$aoi
উদাহরণ 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RemovingConsonants {
public static void main( String args[] ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string: ");
String input = sc.nextLine();
String regex = "([^aeiouyAEIOUY0-9\\W])";
String constants = "";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
//Creating an empty string buffer
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
System.out.println("Result: \n"+ sb.toString() );
}
} আউটপুট
Enter input string: # Hello how are you welcome to Tutorialspoint # Result: # eo o ae you eoe o uoiaoi #