string regex question

M

Marc E

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
 
S

SadRed

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

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);
}
}
 
M

Marc E

ahhhh, the dollar signs. i was using backslashes like you do with matcher.

thanks a million SadRed.


SadRed said:
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

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);
}
}
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top