Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

java regular expression problem

Thread view: 
ranumehta79@gmail.com - 31 Jul 2006 00:35 GMT
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 - 31 Jul 2006 02:15 GMT
ranumehta79@gmail.com のメッセージ:

> Hi,
>
[quoted text clipped - 15 lines]
> 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 - 31 Jul 2006 02:28 GMT
hiwa のメッセージ:

> ranumehta79@gmail.com のメッセージ:
>
[quoted text clipped - 24 lines]
>   }
> }
or,
System.out.println(text.replaceAll("(?:abc){2,}", "def"));
Jeffrey Schwab - 31 Jul 2006 23:07 GMT
> hiwa のメッセージ:
>
[quoted text clipped - 28 lines]
> 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.
ranumehta79@gmail.com - 31 Jul 2006 02:30 GMT
ranumehta79@gmail.com - 31 Jul 2006 03:12 GMT


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.