这个不用正则表达式,用字符串index关键字,再找后面几个字符就可以了
比如取得port:
String str = "oracle=....";
int portIndex =str.indexOf("port");
String port = str.substring(portIndex+5,portIndex+9);
比如取得port:
String str = "oracle=....";
int portIndex =str.indexOf("port");
String port = str.substring(portIndex+5,portIndex+9);
解决方案 »
- 急急急、求助、如何使JList数目不足不能填满JScrollPane时点击空白处不选择最后一项
- 利用字节流和字符流读文件时,为什么读一个字节和读一个字符完全相同。
- JAVA如何开发桌面动画
- 一个很基础性的问题
- 急! JTable页角在我的单位可以打印出来,别的单位打印不出来了,但内容都能正常打印(看代码)。
- 请教:用JAVA2D写的程序,会出现移植性问题吗?
- 关于copy/paste/cut的菜单操作问题
- 问几个问题,看看有没有办法实现,发表你的意见有分!
- 高手请进,有关MVC(模型-视图-控制器)设计的讨论
- 关于jar包的问题!
- java有什么容器可以实现这样的要求?在线等!
- 哪有将exe程序加载为系统服务的工具呀?
这种方式不好,我有很多组要取出来,而不是只有一组。to billh2003(比尔):
不解你的意思。
Thank you anyway.
HOST [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} PORT \\[0-9]+\\SERVICE_NAME \\[a-zA-Z]+
自己把反斜杠去掉(ip)不用去。jdk 1。4http://java.sun.com/docs/books/tutorial/extra/regex/index.html给分啊
import java.util.regex.*;/**
* Description of the Class
*
*@author Administrator
*@created 2003?¨?6??8??
*/
public final class RegexTestHarness { private static String REGEX;
private static String INPUT;
private static BufferedReader br;
private static Pattern pattern;
private static Matcher matcher;
private static boolean found;
/**
* The main program for the RegexTestHarness class
*
*@param argv The command line arguments
*/
public static void main(String[] argv) {
initResources();
processTest();
closeResources();
}
/**
* Description of the Method
*/
private static void initResources() {
try {
br = new BufferedReader(new FileReader("regex.txt"));
} catch (FileNotFoundException fnfe) {
System.out.println("Cannot locate input file! " + fnfe.getMessage());
System.exit(0);
}
try {
REGEX = br.readLine();
INPUT = br.readLine();
} catch (IOException ioe) {} pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT); System.out.println("Current REGEX is: " + REGEX);
System.out.println("Current INPUT is: " + INPUT);
}
/**
* Description of the Method
*/
private static void processTest() {
while (matcher.find()) {
System.out.println("I found the text \"" + matcher.group() +
"\" starting at index " + matcher.start() +
" and ending at index " + matcher.end() + ".");
System.out.println(INPUT.substring(matcher.start(), matcher.end()));
found = true;
} if (!found) {
System.out.println("No match found.");
}
}
/**
* Description of the Method
*/
private static void closeResources() {
try {
br.close();
} catch (IOException ioe) {}
}
}
是我操的的代码建立regex.txt,写入如下二行,第一行为正则表达,自己运行看吧
\\[0-9]+\\
oracle\192.168.10.1\1521\orcl
SERVICE_NAME 用 [a-zA-Z]+$ 找
//只取需要的character
public static String removeSpaceNPunctuation(String str) {
Matcher m = Pattern.compile("[\\d\\w\\(\\)\\.\\=]+").matcher(str);
StringBuffer f = new StringBuffer();
while (m.find()) {
f.append(m.group());
}
return f.toString();
}