Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > string regex question

Reply
Thread Tools

string regex question

 
 
Marc E
Guest
Posts: n/a
 
      04-27-2007
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


 
Reply With Quote
 
 
 
 
SadRed
Guest
Posts: n/a
 
      04-27-2007
On Apr 28, 8:01 am, "Marc E" <(E-Mail Removed)> wrote:
> 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);
}
}

 
Reply With Quote
 
 
 
 
Marc E
Guest
Posts: n/a
 
      04-28-2007
ahhhh, the dollar signs. i was using backslashes like you do with matcher.

thanks a million SadRed.


"SadRed" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Apr 28, 8:01 am, "Marc E" <(E-Mail Removed)> wrote:
>> 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);
> }
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex =~ string or string =~ regex? Ruby Newbee Ruby 3 01-04-2010 06:04 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments