string regex question

Discussion in 'Java' started by Marc E, Apr 28, 2007.

  1. Marc E

    Marc E Guest

    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
    Marc E, Apr 28, 2007
    #1
    1. Advertising

  2. Marc E

    SadRed Guest

    On Apr 28, 8:01 am, "Marc E" <> 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);
    }
    }
    SadRed, Apr 28, 2007
    #2
    1. Advertising

  3. Marc E

    Marc E Guest

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

    thanks a million SadRed.


    "SadRed" <> wrote in message
    news:...
    > On Apr 28, 8:01 am, "Marc E" <> 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);
    > }
    > }
    >
    Marc E, Apr 28, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    731
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,586
    Mladen Adamovic
    Dec 5, 2003
  3. Xah Lee
    Replies:
    1
    Views:
    937
    Ilias Lazaridis
    Sep 22, 2006
  4. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
  5. Ruby Newbee

    regex =~ string or string =~ regex?

    Ruby Newbee, Jan 4, 2010, in forum: Ruby
    Replies:
    3
    Views:
    131
    Kirk Haines
    Jan 4, 2010
Loading...

Share This Page