I do it this way (it works for me):

  /**
   * @param tag The tag without the angle bracket, i.e.,
   *             the given value does not start with "<".
   * @param str The string containing the tags and data.
   * @return empty string when tag absent or str invalid.
   */
  String takeit(String tag,String str){
    try{
      int i = str.indexOf("<"+tag+">");
      if(i<0){
        i = str.indexOf("<"+tag+" ");
      }
      if(i<0) {
        return "";//tag not found
      }
      String s = str.substring(i);
      i = s.indexOf(">");
      if(i<0) {
        //no matching end-angle-bracket (>)
        return "";
      }
      s = s.substring(i+1);//the returned string begins with the data
of this tag
      int j = s.indexOf("</"+tag+">"); //in this version,  "<tag/>"
tags are not used.
      if(j<0) {
        //TODO Log.d(...)
        return "";
      }
      s = s.substring(0,j); // j is the position of the end-tag.
      return s.trim();
    }catch(Exception ex){
      /* TODO maybe
      Log.d("myClass",ex+" - Thread.currentThread()
                   +".takeit() returning empty string;"
                   +" tag = "+tag
                   +"; str = "+str
                 );
      */
      return "";
    }
  }

cheers
serge

On Mar 12, 4:34 pm, EECOLOR <eeco...@gmail.com> wrote:
> I think that would be something like this:
>
> public String takeit(String str, String tag)
> {
>  return str.replace(".*?<" + tag + ">(.*?)</" + tag + ">.*", $1);
>
> }
>
> Greetz Erik
>
> On Thu, Mar 12, 2009 at 7:14 PM, guruk <ilovesi...@gmail.com> wrote:
>
> > Hi,
> > i have a long String and need to capture some text in between of some
> > tags.
>
> > for example:
>
> > myString="Hello, this is a test <start>12345</start> and here i like
> > to say <marki>oioidddad</marki> what is that <opxmark>notwise</
> > opxmark> and now i close";
>
> > How would you do in java regex or any short thing like:
>
> > starttag = takeit(myString,"start");          //result = "12345"
> > marktag =takeit(myString,"marki");         //result = "oioidddad"
> > opxmark=takeit(myString,"opxmark");    //result == "notwise"
>
> > thanks a lot from your java newbie :)
>
> > chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to