Sunday, 2 November 2014

replaceAll() : Replace all the matched sequence with this sequence

public String replaceAll(String replacement)
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.

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

public class RegExHarness {
    public static void main(String args[]) throws IOException{
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        Pattern pattern;
        Matcher matcher;
        
        String regex;
        String text;
              
        System.out.println("Enter Regular Expression");
        regex = br.readLine();
        pattern = Pattern.compile(regex);
        
        while(true){
            try{
                System.out.println("Enter the string");
                text = br.readLine();
                matcher = pattern.matcher(text);

                String newStr = matcher.replaceAll("dummy");
                System.out.println(newStr);
            }
            catch(IOException e){
                System.out.println(e);
            }
        }     
    }
}

Output
Enter Regular Expression
is
Enter the string
This is easy.. is it?
Thdummy dummy easy.. dummy it?
Enter the string




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment