CLUEL3SS
CLUEL3SS

Reputation: 223

Extract text between tags in simple string

I cannot seem to grasp the whole concept of regular expressions, I've been working with php for a couple years now and have, for the most part, tried to avoid the preg_match and such functions. I was wondering if you guys could point me in the right direction to where I might be able to learn regex for java, or just regex in general? I've tried numerous different tutorials and guides and I am still having trouble.

In the mean time while I am trying to learn and improve my regex skills, can you guys help me with this?

in java I have a string that is like so

String secKey;
secKey = "<auth_key>5aff0b2449511aac46e14b5e62436e994c5d</auth_key>";

How would I go about extracting just "5aff0b2449511aac46e14b5e62436e994c5d" from the string?

If you guys could help me with that and possibly point me in the right direction to get me on the right track with regex that would be great, Thanks!

Upvotes: 0

Views: 345

Answers (3)

aalku
aalku

Reputation: 2878

Regular expressions in java using java.util.regex.Patternlink

Regular expressions Tutorial link

Upvotes: 0

mazaneicha
mazaneicha

Reputation: 9417

Something like this:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class X {
        . . .
    public static String extractValue(String s) {
      Pattern p =  Pattern.compile("<auth_key>(.+)</auth_key>") ;
      Matcher m = p.matcher(s) ;
      if ( m.find()) 
          return m.group(1) ;
      else
         return null ;
    }
        . . .
}

Upvotes: 4

zawhtut
zawhtut

Reputation: 8541

I started learning regex from this SO post. Learning Regular Expressions As a learning tool as well as testing ground for current works on regex, I used to use http://rubular.com/ . I hope this will give you a head start.

Upvotes: 1

Related Questions