正则 java 查找 打印 所有匹配项

package com.test.regex;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestRegEx {

/**
* @param args
*/
public static void main(
String[] args) {
String filePath = "E:\\1.sql";
StringBuilder fileContent = FileUtil.readTextFile(filePath);

Pattern p = Pattern.compile("(:\\b\\w*\\b)");
Matcher m = p.matcher(fileContent);

// Find all matches
while (m.find()) {
// Get the matching string
String digitNumList = m.group();
System.out.println(digitNumList);
}

}

}

class FileUtil {

/**
* 读取文本文件
*
* @param filePath
* @return
*/
public static StringBuilder readTextFile(
String filePath) {

File file = new File(filePath);

return readTextFile(file);
}

/**
* 读取文本文件
*
* @param filePath
* @return
*/
public static StringBuilder readTextFile(
File file) {
BufferedReader br = null;
FileReader fr = null;

StringBuilder sb = null;

try {
sb = new StringBuilder();
fr = new FileReader(file);
br = new BufferedReader(fr);
String tempString = null;

while ((tempString = br.readLine()) != null) {
sb.append(tempString);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return sb;
}
}

你可能感兴趣的:(java)