সাধারণ অক্ষর শ্রেণী "[ ]" এর মধ্যে সমস্ত নির্দিষ্ট অক্ষরের সাথে মেলে। নিচের অভিব্যক্তিটি xyz ছাড়া অক্ষরের সাথে মেলে।
"[xyz]"
একইভাবে, নিম্নলিখিত অভিব্যক্তিটি প্রদত্ত ইনপুট স্ট্রিং-এর সমস্ত স্বরবর্ণের সাথে মেলে।
"([^aeiouAEIOU0-9\\W]+)";
তারপর আপনি মিলিত অক্ষরগুলিকে রিপ্লেসঅ্যাল() পদ্ধতি ব্যবহার করে খালি স্ট্রিং “” দিয়ে প্রতিস্থাপন করে অপসারণ করতে পারেন।
উদাহরণ 1
public class RemovingVowels {
public static void main( String args[] ) {
String input = "Hi welcome to tutorialspoint";
String regex = "[aeiouAEIOU]";
String result = input.replaceAll(regex, "");
System.out.println("Result: "+result);
}
} আউটপুট
Result: H wlcm t ttrlspnt
উদাহরণ 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main( String args[] ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string: ");
String input = sc.nextLine();
String regex = "[aeiouAEIOU]";
String constants = "";
System.out.println("Input string: \n"+input);
//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()) {
constants = constants+matcher.group();
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
System.out.println("Result: \n"+ sb.toString()+constants );
}
} আউটপুট
Enter input string: this is a sample text Input string: this is a sample text Result: ths s smpl txtiiaaee