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 / April 2007

Tip: Looking for answers? Try searching our database.

string regex question

Thread view: 
Marc E - 28 Apr 2007 00:01 GMT
is there a way to do regex backreference replacement with the String class
without Pattern and Matcher? for example, I'd like to do something like
this:
String somebigstring = "blahblahblah<hooey>and some stuff here</hooey> and
some other stuff";
String mystring = somebigstring.replace("(.*)<hooey>(*.)</hooey","\2");

such that mystring is then the stuff between the hooey tags.

just seems to me that the 4-5 lines of pattern/matcher code i have to write
to achieve this is way overkill and so i have to believe there's a much
simpler way to do it with String, but i can't get backreferences to work in
replacements. probably missign something stupid, but i can't find it.

thanks a lot.

Marc
SadRed - 28 Apr 2007 00:47 GMT
> is there a way to do regex backreference replacement with the String class
> without Pattern and Matcher? for example, I'd like to do something like
[quoted text clipped - 13 lines]
>
> Marc

public class MarcE{

 public static void main(String[] args){
   String somebigstring =
    "blahblahblah<hooey>and some stuff here</hooey> and some other
stuff";

   String mystring = somebigstring.substring
     (somebigstring.indexOf("<hooey>") +"<hooey>".length(),
      somebigstring.indexOf("</hooey>"));

   String mystring2 = somebigstring.replaceAll
     ("^(.*?)<hooey>(.*?)</hooey>(.*?)$", "$2");

   System.out.println(mystring);
   System.out.println(mystring2);
 }
}
Marc E - 28 Apr 2007 03:37 GMT
ahhhh, the dollar signs.  i was using backslashes like you do with matcher.

thanks a million SadRed.

>> is there a way to do regex backreference replacement with the String
>> class
[quoted text clipped - 36 lines]
>  }
> }


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.