java regular expression problem

R

ranumehta79

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
 
H

hiwa

(e-mail address removed) ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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"));
}
}
 
H

hiwa

hiwa ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
(e-mail address removed) ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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"));
 
J

Jeffrey Schwab

hiwa said:
hiwa ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
(e-mail address removed) ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top