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!
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 ;
}
. . .
}
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.
Regular expressions in java using java.util.regex.Pattern
link
Regular expressions Tutorial link
精彩评论