Data Extraction এবং Text Manipulation

Practical Use Cases এবং Industry Applications - জাভা রেজেক্স (Java Regex) - Java Technologies

312

Java Reflection Package (java.lang.reflect) মূলত রানটাইমে ক্লাস, ফিল্ড, মেথড এবং কনস্ট্রাক্টর ইত্যাদি ম্যানিপুলেট করতে ব্যবহৃত হয়। তবে Data Extraction এবং Text Manipulation সাধারণত I/O Operations এবং String Handling এর মাধ্যমে সম্পন্ন হয়। এসব ক্ষেত্রে Reflection সরাসরি ব্যবহৃত না হলেও, Reflection প্যাকেজের ব্যবহার দিয়ে dynamic data extraction বা dynamic field access করা যেতে পারে, যা ডেটা ম্যানিপুলেশনকে আরও শক্তিশালী করে তোলে।

এখানে আমি Data Extraction এবং Text Manipulation নিয়ে আলোচনা করব এবং দেখাব কিভাবে Java Reflection এবং অন্যান্য Java টেকনোলজি ব্যবহার করে এই কাজগুলো করা যায়।

1. Data Extraction Using Java Reflection

ডাইনামিকভাবে Data Extraction করার জন্য Java Reflection ব্যবহার করতে হলে, সাধারণত আপনাকে Reflection API এর সাহায্যে ক্লাসের ফিল্ড বা মেথডের তথ্য অ্যাক্সেস করতে হয়। এক্ষেত্রে, Reflection ডেটার স্ট্রাকচার এবং এর মধ্যে থাকা মান বের করার জন্য কার্যকরী হতে পারে।

Data Extraction Example:

ধরা যাক, আমাদের একটি ক্লাস আছে যার মধ্যে কিছু প্রাইভেট ফিল্ড এবং মেথড রয়েছে, এবং আমরা Reflection ব্যবহার করে তাদের মান বের করতে চাই।

import java.lang.reflect.*;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class DataExtractionExample {
    public static void main(String[] args) throws Exception {
        // Create an object of Person class
        Person person = new Person("John Doe", 30);

        // Get the Class object for Person
        Class<?> personClass = person.getClass();

        // Access private field 'name' using reflection
        Field nameField = personClass.getDeclaredField("name");
        nameField.setAccessible(true);  // Allow access to private field
        String name = (String) nameField.get(person);  // Extract the value of 'name'

        // Access private field 'age' using reflection
        Field ageField = personClass.getDeclaredField("age");
        ageField.setAccessible(true);  // Allow access to private field
        int age = (int) ageField.get(person);  // Extract the value of 'age'

        // Print extracted data
        System.out.println("Extracted Data: ");
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);

        // Access private method 'displayInfo' using reflection
        Method displayInfoMethod = personClass.getDeclaredMethod("displayInfo");
        displayInfoMethod.setAccessible(true);  // Allow access to private method
        displayInfoMethod.invoke(person);  // Invoke the method dynamically
    }
}

ব্যাখ্যা:

  1. Field এবং Method Reflection: আমরা getDeclaredField() এবং getDeclaredMethod() ব্যবহার করে ক্লাসের প্রাইভেট ফিল্ড এবং মেথড অ্যাক্সেস করেছি।
  2. setAccessible(true): এই মেথডটি প্রাইভেট ফিল্ড বা মেথডের অ্যাক্সেসের অনুমতি দেয়, কারণ সেগুলি সাধারণভাবে বাইরের কোডের জন্য অ্যাক্সেসযোগ্য নয়।
  3. Field.get(): এটি ফিল্ডের মান বের করার জন্য ব্যবহার করা হয়।

আউটপুট:

Extracted Data: 
Name: John Doe
Age: 30
Name: John Doe, Age: 30

2. Text Manipulation Using Java

Java-তে Text Manipulation করা হয় String ক্লাস এবং অন্যান্য ইউটিলিটি ক্লাস ব্যবহার করে। আমরা এখানে Text Manipulation এর মাধ্যমে স্ট্রিংয়ের কিছু অপারেশন যেমন substring extraction, text replacement, string formatting ইত্যাদি দেখব।

Text Manipulation Examples:

i. Substring Extraction

public class TextManipulationExample {
    public static void main(String[] args) {
        String text = "Hello, welcome to Java!";
        
        // Extract substring from text
        String extracted = text.substring(7, 14); // Extract "welcome"
        
        System.out.println("Extracted Substring: " + extracted);
    }
}

আউটপুট:

Extracted Substring: welcome

ii. Text Replacement

public class TextManipulationExample {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog.";
        
        // Replace word "quick" with "slow"
        String replacedText = text.replace("quick", "slow");
        
        System.out.println("Replaced Text: " + replacedText);
    }
}

আউটপুট:

Replaced Text: The slow brown fox jumps over the lazy dog.

iii. String Formatting

public class TextManipulationExample {
    public static void main(String[] args) {
        String name = "John";
        int age = 30;
        
        // Format string
        String formattedText = String.format("Hello, my name is %s and I am %d years old.", name, age);
        
        System.out.println(formattedText);
    }
}

আউটপুট:

Hello, my name is John and I am 30 years old.

iv. Converting String to Uppercase/Lowercase

public class TextManipulationExample {
    public static void main(String[] args) {
        String text = "Hello World!";
        
        // Convert to uppercase
        String upperText = text.toUpperCase();
        System.out.println("Uppercase: " + upperText);
        
        // Convert to lowercase
        String lowerText = text.toLowerCase();
        System.out.println("Lowercase: " + lowerText);
    }
}

আউটপুট:

Uppercase: HELLO WORLD!
Lowercase: hello world!

3. Complex Text Manipulation with Regex

Java Regex ব্যবহার করে জটিল টেক্সট ম্যানিপুলেশন করা সম্ভব, যেমন pattern matching, substitution, extraction ইত্যাদি।

Regex with Text Manipulation Example:

ধরা যাক, আমরা একটি টেক্সট থেকে email addresses বের করতে চাই এবং প্রতিটি ইমেইলের আগে "Contact: " এই ট্যাগ যোগ করতে চাই।

import java.util.regex.*;
import java.util.*;

public class RegexTextManipulation {
    public static void main(String[] args) {
        String text = "You can contact us at user@example.com or support@company.org.";

        // Regex to match email addresses
        String emailRegex = "[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}";

        // Compile the regex
        Pattern pattern = Pattern.compile(emailRegex);
        Matcher matcher = pattern.matcher(text);

        // Create a list to store modified email addresses
        List<String> modifiedEmails = new ArrayList<>();

        // Find all email addresses and add "Contact: " before them
        while (matcher.find()) {
            modifiedEmails.add("Contact: " + matcher.group());
        }

        // Print the modified emails
        System.out.println("Modified Email Addresses:");
        modifiedEmails.forEach(System.out::println);
    }
}

আউটপুট:

Modified Email Addresses:
Contact: user@example.com
Contact: support@company.org

ব্যাখ্যা:

  1. Pattern.compile(): Regex প্যাটার্ন তৈরি করতে ব্যবহৃত হয়।
  2. Matcher.find(): এটি টেক্সটের মধ্যে ইমেইল ঠিকানাগুলি খুঁজে বের করতে ব্যবহৃত হয়।
  3. matcher.group(): এটি ম্যাচ হওয়া ইমেইল ঠিকানাটি ফেরত দেয়।

  • Data Extraction: Java Reflection ব্যবহার করে আপনি যেকোনো ক্লাস বা অবজেক্ট থেকে ডাইনামিকভাবে ডেটা অ্যাক্সেস এবং এক্সট্র্যাক্ট করতে পারেন।
  • Text Manipulation: Java String এবং Regex এর মাধ্যমে সহজেই স্ট্রিং ম্যানিপুলেশন এবং প্যাটার্ন ম্যাচিং করা সম্ভব, যা ডেটা ক্লিনিং এবং ফরম্যাটিং এর জন্য কার্যকরী।
  • Complex Text Manipulation with Regex: Regex ব্যবহার করে জটিল টেক্সট ম্যানিপুলেশন যেমন ইনপুট ভ্যালিডেশন, সাবস্টিটিউশন এবং এক্সট্র্যাকশন করা যেতে পারে।

এটি Java-তে টেক্সট প্রসেসিং এবং ডেটা ম্যানিপুলেশন করার জন্য একটি শক্তিশালী পদ্ধতি যা Reflection, String Handling এবং Regex ব্যবহার করে আরও কার্যকরীভাবে বাস্তবায়িত হতে পারে।

Content added By
Promotion

Are you sure to start over?

Loading...