Extracting a string

Discussion in 'Java' started by Mar Thomas, Sep 26, 2003.

  1. Mar Thomas

    Mar Thomas Guest

    Here's my question

    How can I retrieve only stuff1's value from a processing instruction?

    <?target stuff1="1-100" stuff2="200.0 ?>
     
    Mar Thomas, Sep 26, 2003
    #1
    1. Advertising

  2. Mar Thomas

    Sergio Juan Guest

    Hi

    The easy way (but you can only apply it if you are sure that the message
    will have this format)

    java.util.StringTokenizer str = new java.util.StringTokenizer("<?target
    stuff1=\"1-100\" stuff2=\"200.0\" ?>", "\"");
    str.nextToken(); // discard the first part of the substring, that will be
    <?target stuff1=
    String myString = str.nextToken(); // Here you got it.

    If the String can change slightly, you may want to look for a regular
    expression parser (jakarta has one called ORO, I think), or even a grammar
    parser, as the solution I wrote will fail if the order of the attributes is
    altered.

    Regards.

    "Mar Thomas" <> escribió en el mensaje
    news:bl1fd4$d6j$-nexis.com...
    > Here's my question
    >
    > How can I retrieve only stuff1's value from a processing instruction?
    >
    > <?target stuff1="1-100" stuff2="200.0 ?>
    >
    >
     
    Sergio Juan, Sep 26, 2003
    #2
    1. Advertising

  3. Mar Thomas

    Mar Thomas Guest

    Thanks..Your solution gives me an idea


    "Sergio Juan" <> wrote in message
    news:m3Ycb.109271$...
    > Hi
    >
    > The easy way (but you can only apply it if you are sure that the message
    > will have this format)
    >
    > java.util.StringTokenizer str = new java.util.StringTokenizer("<?target
    > stuff1=\"1-100\" stuff2=\"200.0\" ?>", "\"");
    > str.nextToken(); // discard the first part of the substring, that will be
    > <?target stuff1=
    > String myString = str.nextToken(); // Here you got it.
    >
    > If the String can change slightly, you may want to look for a regular
    > expression parser (jakarta has one called ORO, I think), or even a grammar
    > parser, as the solution I wrote will fail if the order of the attributes

    is
    > altered.
    >
    > Regards.
    >
    > "Mar Thomas" <> escribió en el mensaje
    > news:bl1fd4$d6j$-nexis.com...
    > > Here's my question
    > >
    > > How can I retrieve only stuff1's value from a processing instruction?
    > >
    > > <?target stuff1="1-100" stuff2="200.0 ?>
    > >
    > >

    >
    >
     
    Mar Thomas, Sep 26, 2003
    #3
  4. Mar Thomas wrote:

    > Here's my question
    >
    > How can I retrieve only stuff1's value from a processing instruction?
    >
    > <?target stuff1="1-100" stuff2="200.0 ?>


    Using DOM? Using SAX? Parsing it with custom code?

    And what exactly do you mean by "retrieve only stuff1's value"? As
    opposed to both stuff1's and stuff2's? Or as opposed to stuff1's name
    and value? Or as opposed to some other alternative?

    If you have an implementation now that isn't working as you'd like then
    it would be very helpful if you would show the code.


    John Bollinger
     
    John C. Bollinger, Sep 26, 2003
    #4
  5. Mar Thomas

    VisionSet Guest

    "Mar Thomas" <> wrote in message
    news:bl1fd4$d6j$-nexis.com...
    > Here's my question
    >
    > How can I retrieve only stuff1's value from a processing instruction?
    >
    > <?target stuff1="1-100" stuff2="200.0 ?>
    >


    Or the regex alternative:

    String input = "<?target stuff1=\"1-100\" stuff2=\"200.0 ?>";

    Matcher matcher =
    Pattern.compile(".*stuff1=\"([^\"]*)\".*").matcher(input);

    matcher.matches();

    System.out.println(matcher.group(1));


    --
    Mike W
     
    VisionSet, Sep 26, 2003
    #5
  6. Mar Thomas

    Mar Thomas Guest

    "John C. Bollinger" <> wrote in message
    news:bl1jut$448$...
    > Mar Thomas wrote:
    >
    > > Here's my question
    > >
    > > How can I retrieve only stuff1's value from a processing instruction?
    > >
    > > <?target stuff1="1-100" stuff2="200.0 ?>

    >
    > Using DOM? Using SAX? Parsing it with custom code?


    Not with DOM or SAX.

    >
    > And what exactly do you mean by "retrieve only stuff1's value"? As
    > opposed to both stuff1's and stuff2's?


    Exactly what I meant. stuff1's value which in this case would be 1-100. The
    reason why I use the sentence "only stuff1" does NOT mean both stuff1 and
    stuff2

    Or as opposed to stuff1's name
    > and value? Or as opposed to some other alternative?


    Stuff1's value is stuff1's value not name...Does it say name anywhere in the
    question?

    Lastly, RTFQ


    >
    > If you have an implementation now that isn't working as you'd like then
    > it would be very helpful if you would show the code.
    >
    >
    > John Bollinger
    >
    >
     
    Mar Thomas, Sep 26, 2003
    #6
  7. Mar Thomas

    Roedy Green Guest

    On Fri, 26 Sep 2003 09:36:36 -0400, "Mar Thomas" <>
    wrote or quoted :

    >How can I retrieve only stuff1's value from a processing instruction?
    >
    ><?target stuff1="1-100" stuff2="200.0 ?>


    1.use an XML parser.
    2. use a regex. See http://mindprod.com/jgloss/regex.html


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 26, 2003
    #7
  8. Mar Thomas wrote:

    > "John C. Bollinger" <> wrote in message
    > news:bl1jut$448$...
    >
    >>Mar Thomas wrote:
    >>
    >>
    >>>Here's my question
    >>>
    >>>How can I retrieve only stuff1's value from a processing instruction?
    >>>
    >>><?target stuff1="1-100" stuff2="200.0 ?>


    > Not with DOM or SAX.
    >
    >
    >>And what exactly do you mean by "retrieve only stuff1's value"? As
    >>opposed to both stuff1's and stuff2's?

    >
    >
    > Exactly what I meant. stuff1's value which in this case would be 1-100. The
    > reason why I use the sentence "only stuff1" does NOT mean both stuff1 and
    > stuff2
    >
    > Or as opposed to stuff1's name
    >
    >>and value? Or as opposed to some other alternative?

    >
    >
    > Stuff1's value is stuff1's value not name...Does it say name anywhere in the
    > question?
    >
    > Lastly, RTFQ


    Well, RTFR! I did not suggest that you might also want stuff1's name, I
    was asking whether that was what you were specifically trying to avoid.
    It is customary to at least make an attempt at solving a problem
    yourself before appealing to this newsgroup for assistance, so my line
    of questioning was aimed at trying to determine the nature of the
    difficulty with your current code so as to give you a response well
    suited to your need. In the future I'll try to remember to not make
    such effort on your behalf.

    For what it's worth, a regex would be a good tool for a task like this.


    John Bollinger
     
    John C. Bollinger, Sep 26, 2003
    #8
  9. Mar Thomas

    Chorg Guest

    On Fri, 26 Sep 2003 09:36:36 -0400, Mar Thomas <> wrote:
    > Here's my question
    >
    > How can I retrieve only stuff1's value from a processing instruction?
    >
    ><?target stuff1="1-100" stuff2="200.0 ?>


    you said you were a perl guy before, and fortunately for us...

    http://javaalmanac.com/egs/java.util.regex/pkg.html

    you can drag good ol regex with you buddy ;)
     
    Chorg, Jan 15, 2004
    #9
  10. Mar Thomas

    Tony Morris Guest

    There are multiple XML (assuming that is what it is) parsing implementations
    that use either SAX or DOM.
    As of 1.4, an implementation is provided for each of these.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)


    "Chorg" <> wrote in message
    news:sLoNb.29983$...
    > On Fri, 26 Sep 2003 09:36:36 -0400, Mar Thomas <> wrote:
    > > Here's my question
    > >
    > > How can I retrieve only stuff1's value from a processing instruction?
    > >
    > ><?target stuff1="1-100" stuff2="200.0 ?>

    >
    > you said you were a perl guy before, and fortunately for us...
    >
    > http://javaalmanac.com/egs/java.util.regex/pkg.html
    >
    > you can drag good ol regex with you buddy ;)
     
    Tony Morris, Jan 15, 2004
    #10
    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. R
    Replies:
    2
    Views:
    529
    Jon A. Cruz
    Jul 25, 2003
  2. Zoran Cutura

    Re: extracting string

    Zoran Cutura, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    722
    Zoran Cutura
    Jun 26, 2003
  3. Ben Pfaff

    Re: extracting string

    Ben Pfaff, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    694
    Ben Pfaff
    Jun 26, 2003
  4. Martien Verbruggen

    Re: extracting string

    Martien Verbruggen, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    727
    Martien Verbruggen
    Jun 26, 2003
  5. CBFalconer

    Re: extracting string

    CBFalconer, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    713
    CBFalconer
    Jun 26, 2003
Loading...

Share This Page