http://download.oracle.com/javase/6/docs/api/java/util/regex/package-summary.html
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegEx {
/**
* @param args
*/
public static void main(String[] args) {
String input = "dbuser:passwd@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(Host=dbhost)(PORT=1520)))(CONNECT_DATA=(SID=MY_DB)))";
getParam(input, "Host");
getParam(input, "HOST");
// getParam(input,"xHOST");
Pattern pattern = Pattern.compile("(\\w+)@(\\w+)/(\\S+)");
Matcher matcher = pattern.matcher("username@hostname//dir1/dir2");
if (matcher.matches()) {
System.out.println("User " + matcher.group(1));
System.out.println("Host " + matcher.group(2));
System.out.println("Dir " + matcher.group(3));
}
}
public static String getParam(String input, String paramName) {
Pattern pattern = Pattern.compile(paramName + "=(\\w+)",
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
System.out.println("Matching " + pattern.pattern());
if (matcher.find()) {
System.out.println("Matched " + matcher.group(1) + " in "
+ matcher.group(0));
return matcher.group(1);
} else {
throw new RuntimeException("Cannot find db connection parameter "
+ paramName + " in " + input);
}
}
}
No comments:
Post a Comment