String.replaceAll() এবং String.replaceFirst() এর মাধ্যমে Text Replace করা

String এর সাথে Regex ব্যবহার - জাভা রেজেক্স (Java Regex) - Java Technologies

271

Java Reflection Package (java.lang.reflect) সাধারণত Pattern এবং Matcher ক্লাসের সাথে কাজ করে regular expressions ব্যবহারের জন্য, কিন্তু String.replaceAll() এবং String.replaceFirst() মেথডগুলি সরাসরি স্ট্রিং-এ টেক্সট রিপ্লেস করার জন্য ব্যবহৃত হয়। এগুলি রেগুলার এক্সপ্রেশন বা সাধারণ স্ট্রিং প্যাটার্নের মাধ্যমে টেক্সট রিপ্লেস করতে পারে, যা আপনাকে স্ট্রিং ম্যানিপুলেশনের ক্ষমতা প্রদান করে।

1. String.replaceAll() Method:

replaceAll() মেথডটি একটি স্ট্রিংয়ের সমস্ত অংশ যেগুলি একটি নির্দিষ্ট রেগুলার এক্সপ্রেশন বা প্যাটার্নের সাথে মেলে, সেগুলিকে নতুন স্ট্রিং দ্বারা রিপ্লেস করে। এটি একটি regex প্যাটার্ন নেয় এবং টেক্সটের সব মিল পাওয়া অংশকে রিপ্লেস করে।

Syntax:

String result = originalString.replaceAll(String regex, String replacement);
  • regex: এটি একটি রেগুলার এক্সপ্রেশন প্যাটার্ন যা আপনি রিপ্লেস করতে চান এমন অংশ চিহ্নিত করবে।
  • replacement: এটি নতুন স্ট্রিং যা মিল পাওয়া অংশের স্থানে রাখা হবে।

Example:

public class ReplaceAllExample {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog.";

        // Replace all occurrences of 'the' (case-insensitive)
        String result = text.replaceAll("(?i)the", "a");

        System.out.println(result);  // Output: "A quick brown fox jumps over a lazy dog."
    }
}

ব্যাখ্যা:

  • replaceAll("(?i)the", "a"): এটি "the" শব্দটি সমস্ত ক্ষেত্রে (case-insensitive) "a" দিয়ে রিপ্লেস করে। (?i) ফ্ল্যাগটি case-insensitive matching সক্রিয় করে।

Output:

A quick brown fox jumps over a lazy dog.

2. String.replaceFirst() Method:

replaceFirst() মেথডটি স্ট্রিংয়ের প্রথম মিল পাওয়া অংশটিকে রিপ্লেস করে। এটি একটি regex প্যাটার্ন নেয় এবং প্রথম ম্যাচিং অংশটি নতুন স্ট্রিং দ্বারা রিপ্লেস করে।

Syntax:

String result = originalString.replaceFirst(String regex, String replacement);
  • regex: এটি একটি রেগুলার এক্সপ্রেশন প্যাটার্ন যা আপনি রিপ্লেস করতে চান এমন অংশ চিহ্নিত করবে।
  • replacement: এটি নতুন স্ট্রিং যা প্রথম মিল পাওয়া অংশের স্থানে রাখা হবে।

Example:

public class ReplaceFirstExample {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog.";

        // Replace the first occurrence of 'the' (case-insensitive)
        String result = text.replaceFirst("(?i)the", "a");

        System.out.println(result);  // Output: "A quick brown fox jumps over the lazy dog."
    }
}

ব্যাখ্যা:

  • replaceFirst("(?i)the", "a"): এটি "the" শব্দের প্রথম মিলটিকে রিপ্লেস করে। (?i) ফ্ল্যাগটি case-insensitive matching সক্রিয় করে।

Output:

A quick brown fox jumps over the lazy dog.

তুলনা: replaceAll() এবং replaceFirst()

MethodDescriptionExampleOutput
replaceAll()সমস্ত ম্যাচিং অংশকে রিপ্লেস করে।replaceAll("the", "a")"A quick brown fox jumps over A lazy dog."
replaceFirst()প্রথম ম্যাচিং অংশটিকে রিপ্লেস করে।replaceFirst("the", "a")"A quick brown fox jumps over the lazy dog."
  • replaceAll(): স্ট্রিংয়ের সমস্ত অংশের সাথে মিলিয়ে রিপ্লেস করে।
  • replaceFirst(): স্ট্রিংয়ের প্রথম মিল পাওয়া অংশটি রিপ্লেস করে।

Advanced Example with Regular Expressions:

Example: Using replaceAll() to Remove Non-Alphanumeric Characters

public class ReplaceAllAdvancedExample {
    public static void main(String[] args) {
        String text = "Hello, @world! How's it going?";

        // Remove all non-alphanumeric characters using regex
        String result = text.replaceAll("[^a-zA-Z0-9\\s]", "");

        System.out.println(result);  // Output: "Hello world Hows it going"
    }
}

ব্যাখ্যা:

  • [^a-zA-Z0-9\\s]: এই regex প্যাটার্নটি স্ট্রিং থেকে সমস্ত অ্যালফানিউমেরিক (a-z, A-Z, 0-9) এবং সাদা স্পেস ছাড়া অন্যান্য সমস্ত চরিত্র (যেমন @, !, ') মুছে ফেলবে।

Output:

Hello world Hows it going

Reflection ব্যবহার করে replaceAll() এবং replaceFirst() মেথড অ্যাক্সেস:

যেহেতু replaceAll() এবং replaceFirst() মেথডগুলি সাধারণ স্ট্রিং অপারেশন, সেগুলি Reflection দ্বারা অ্যাক্সেস করা যেতে পারে যদি আপনি একাধিক স্ট্রিংয়ের উপর একই ধরনের অপারেশন প্রয়োগ করতে চান।

Example: Using Reflection to Invoke replaceAll() Method:

import java.lang.reflect.*;

public class ReflectionReplaceAllExample {
    public static void main(String[] args) throws Exception {
        String text = "Hello, @world! How's it going?";

        // Get the 'replaceAll' method using reflection
        Method replaceAllMethod = String.class.getMethod("replaceAll", String.class, String.class);

        // Use reflection to invoke the method
        String result = (String) replaceAllMethod.invoke(text, "[^a-zA-Z0-9\\s]", "");

        System.out.println(result);  // Output: "Hello world Hows it going"
    }
}

ব্যাখ্যা:

  • Reflection এর মাধ্যমে replaceAll() মেথডকে ডাইনামিকভাবে কল করা হয়েছে। এখানে String.class.getMethod("replaceAll", String.class, String.class) মেথডটি replaceAll() মেথডের রেফারেন্স দেয় এবং invoke() এর মাধ্যমে এটি চালানো হয়।

Output:

Hello world Hows it going

  • replaceAll() এবং replaceFirst() দুটি মেথডই স্ট্রিংয়ের মধ্যে প্যাটার্ন অনুসারে টেক্সট রিপ্লেস করতে ব্যবহৃত হয়।
    • replaceAll(): সমস্ত ম্যাচিং অংশকে রিপ্লেস করে।
    • replaceFirst(): প্রথম ম্যাচিং অংশটিকে রিপ্লেস করে।
  • রেগুলার এক্সপ্রেশন প্যাটার্ন এবং Reflection এর মাধ্যমে এই মেথডগুলো আরও শক্তিশালী এবং ফ্লেক্সিবল হতে পারে।
Content added By
Promotion

Are you sure to start over?

Loading...