Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
import java.util.regex.Matcher;
import java.util.regex.Pattern;

    public class Regex {

        public static void main(String args[]){     

            Pattern p = Pattern.compile(".*?(cat).*?(dog)?.*?(tiger)");
            String input = "The cat is a tiger";
            Matcher m = p.matcher(input);
            StringBuffer str = new StringBuffer();
            if (m.find()) {

            //In the output i want to replace input string with group 3 with group 1 value and group 2 with cow. Though group2 is present or not.
//i.e. group 2 is null
            }
        }
    }

I'd like to know whether it is possible in java to use regex to replace an input string with a particular value for captured groups.

Please help

share|improve this question
    
I'm having difficulty understanding your requirements. Can you give us a couple of examples to illustrate? For example, what do you expect the output to be when the input is "The cat is a tiger"? And what do you expect the output to be when the input is "My dog ate a tiger"? –  David Wallace Nov 6 '13 at 10:51
1  
Please elaborate your requirements, if you know what has to be replaced and with what value, you can use String.replace(target, replacement) –  lazyprogrammer Nov 6 '13 at 10:53
    
i want output like this. "The cat is a cow tiger". (Though the sentence is meaningless. i'm here explaining context. ) Also if input is "The cat is a dog and a tiger. Then output will be " The cat is a cow and a cat" –  Mani Nov 6 '13 at 10:54
    
so did anything help? –  lazyprogrammer Nov 6 '13 at 11:24
add comment

2 Answers

The String class's replace and replaceAll methods are the best way to do this. They support regex strings as the search argument.

share|improve this answer
    
replace() actually replaces each EXACT match of the given string or char. replaceFirst() and replaceAll() can be used with regex. –  grexter89 Nov 6 '13 at 11:30
add comment
Pattern p = Pattern.compile("(cat)(.*?)(dog)?(.*?)(tiger)");
String input = "The cat is a tiger";
Matcher m = p.matcher(input);
StringBuffer str = new StringBuffer();
while(m.find())
{
  m.appendReplacement(str, "$5$2$3$4$1");
}
m.appendTail(str);
System.out.println(str);

By the way if it doesn’t matter whether there’s a dog you can simplify it:

Pattern p = Pattern.compile("(cat)(.*?)(tiger)");
String input = "The cat is a tiger";
Matcher m = p.matcher(input);
StringBuffer str = new StringBuffer();
while(m.find())
{
  m.appendReplacement(str, "$3$2$1");
}
m.appendTail(str);
share|improve this answer
    
Just as a side note: That’s the equivalent to String result="The cat is a tiger".replaceAll("(cat)(.*?)(dog)?(.*?)(tiger)", "$5$2$3$4$1"); so if you want to do it just once that single line will do too. –  Holger Nov 6 '13 at 11:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.