java regular expression problem  
Author Message
ranumehta79





PostPosted: 2006-7-31 7:35:00 Top

java-programmer, java regular expression problem Hi,

I want to replace mulitple occurances of a string one after the another
with a single string

for eg .

input : a hello b world c java abcabcabc
output: a hello b world c java def

I mean multiple occurances of pattern 'abc' with a single string 'def'.


how can do this with JAVA regular expressions ?

I tried replaceAll method in string class, but I dont know how to
format regular expression.

thanks
Ranu

 
hiwa





PostPosted: 2006-7-31 9:16:00 Top

java-programmer >> java regular expression problem email***@***.com 銇儭銉冦偦銉笺偢:

> Hi,
>
> I want to replace mulitple occurances of a string one after the another
> with a single string
>
> for eg .
>
> input : a hello b world c java abcabcabc
> output: a hello b world c java def
>
> I mean multiple occurances of pattern 'abc' with a single string 'def'.
>
>
> how can do this with JAVA regular expressions ?
>
> I tried replaceAll method in string class, but I dont know how to
> format regular expression.
>
> thanks
> Ranu
public class Ranu{
public static void main(String[] args){
String text = "a hello b world c java abcabcabc";

System.out.println(text.replaceAll("(?:abc)+", "def"));
}
}

 
hiwa





PostPosted: 2006-7-31 9:28:00 Top

java-programmer >> java regular expression problem hiwa 銇儭銉冦偦銉笺偢:

> email***@***.com 銇儭銉冦偦銉笺偢:
>
> > Hi,
> >
> > I want to replace mulitple occurances of a string one after the another
> > with a single string
> >
> > for eg .
> >
> > input : a hello b world c java abcabcabc
> > output: a hello b world c java def
> >
> > I mean multiple occurances of pattern 'abc' with a single string 'def'.
> >
> >
> > how can do this with JAVA regular expressions ?
> >
> > I tried replaceAll method in string class, but I dont know how to
> > format regular expression.
> >
> > thanks
> > Ranu
> public class Ranu{
> public static void main(String[] args){
> String text = "a hello b world c java abcabcabc";
>
> System.out.println(text.replaceAll("(?:abc)+", "def"));
> }
> }
or,
System.out.println(text.replaceAll("(?:abc){2,}", "def"));

 
 
ranumehta79





PostPosted: 2006-7-31 9:30:00 Top

java-programmer >> java regular expression problem thanks

 
 
ranumehta79





PostPosted: 2006-7-31 10:12:00 Top

java-programmer >> java regular expression problem thanks

 
 
Jeffrey Schwab





PostPosted: 2006-8-1 6:08:00 Top

java-programmer >> java regular expression problem hiwa wrote:
> hiwa 銇儭銉冦偦銉笺偢:
>
>> email***@***.com 銇儭銉冦偦銉笺偢:
>>
>>> Hi,
>>>
>>> I want to replace mulitple occurances of a string one after the another
>>> with a single string
>>>
>>> for eg .
>>>
>>> input : a hello b world c java abcabcabc
>>> output: a hello b world c java def
>>>
>>> I mean multiple occurances of pattern 'abc' with a single string 'def'.
>>>
>>>
>>> how can do this with JAVA regular expressions ?
>>>
>>> I tried replaceAll method in string class, but I dont know how to
>>> format regular expression.
>>>
>>> thanks
>>> Ranu
>> public class Ranu{
>> public static void main(String[] args){
>> String text = "a hello b world c java abcabcabc";
>>
>> System.out.println(text.replaceAll("(?:abc)+", "def"));
>> }
>> }
> or,
> System.out.println(text.replaceAll("(?:abc){2,}", "def"));

For clarity to the OP: This version replaces multiple occurrences, but
not a stand-alone occurrence. E.g., XabcabcX becomes XdefX, but XabcX
remains XabcX.