使用 Java StringBuilder 的正则表达式
Regular Expression Using Java StringBuilder
当我执行这个程序时,它运行得非常好
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main( String args[] ) {
// String to be scanned to find the pattern.
String line = "0X10001,0X10002,0X610001,0X610002";
String pattern = "0X(?=\d{6})|(0)X(?=\d{5})";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
StringBuffer builder = new StringBuffer();
while (m.find()) {
m.appendReplacement(builder,
"");
}
m.appendTail(builder);
// Print the replaced matcher
System.out.println("After Replacement: "
+ builder.toString());
}
}
但是当我将同一段代码合并到下面的代码中时,它会抛出错误。 (此代码将原始数据写入 CSV 文件,但在写入之前进行十六进制清理。)
public static String writeToCSV(StringBuilder rawData, String granularity, String threadName, long collectionTime)
throws IOException {
StringBuilder fileName = new StringBuilder();
fileName.append(String.valueOf(System.currentTimeMillis())).append("_").append(threadName).append(".csv");
StringBuilder directoryPath = new StringBuilder();
directoryPath.append(TEMP_DATA_DIR_PATH).append(granularity).append(File.separator).append(collectionTime);
AmpStatsDataUtil.createWritableDirectory(directoryPath.toString());
StringBuilder filePath = new StringBuilder();
filePath.append(directoryPath.toString()).append(File.separator).append(fileName.toString());
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath.toString(), true), "UTF-8"));
// Hexadecimal clean up
Pattern r = Pattern.compile("0x(?=\d{6})|(0)x(?=\d{5})");
Matcher m = r.matcher(rawData);
StringBuffer builder = new StringBuffer();
while (m.find()) {
m.appendReplacement(builder,"");
}
m.appendTail(builder);
LOGGER.info("Cleaned hexadecimal digits are {0}",builder.toString());
try {
bw.write(builder.toString());
} catch (IOException e) {
LOGGER.error("Error while writing data to CSV file:{0}", e);
} finally {
try {
if (bw != null) {
bw.flush();
}
bw.close();
} catch (IOException e) {
LOGGER.error("Error while closing bufferedwriter:{0}", e);
}
}
return fileName.toString();
}
但不使用字符串生成器它工作正常:
//code 3
public static String writeToCSV(StringBuilder rawData, String granularity, String threadName, long collectionTime)
throws IOException {
StringBuilder fileName = new StringBuilder();
fileName.append(String.valueOf(System.currentTimeMillis())).append("_").append(threadName).append(".csv");
StringBuilder directoryPath = new StringBuilder();
directoryPath.append(TEMP_DATA_DIR_PATH).append(granularity).append(File.separator).append(collectionTime);
AmpStatsDataUtil.createWritableDirectory(directoryPath.toString());
StringBuilder filePath = new StringBuilder();
filePath.append(directoryPath.toString()).append(File.separator).append(fileName.toString());
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath.toString(), true), "UTF-8"));
// Hexadecimal clean up
Pattern myPattern = Pattern.compile("0x");
Matcher myMatcher = myPattern.matcher(rawData);
try {
bw.write(myMatcher.repalaceAll("0");
} catch (IOException e) {
LOGGER.error("Error while writing data to CSV file:{0}", e);
} finally {
try {
if (bw != null) {
bw.flush();
}
bw.close();
} catch (IOException e) {
LOGGER.error("Error while closing bufferedwriter:{0}", e);
}
}
return fileName.toString();
}
你能告诉我为什么第二段代码不能编译吗?
从Javadoc for Matcher
可以看出appendReplacement
和appendTail
方法只使用了StringBuffer
:
public Matcher appendReplacement(StringBuffer sb, String replacement)
public StringBuffer appendTail(StringBuffer sb)
因此,如果要编译,则需要使用 StringBuffer
版本。
当我执行这个程序时,它运行得非常好
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main( String args[] ) {
// String to be scanned to find the pattern.
String line = "0X10001,0X10002,0X610001,0X610002";
String pattern = "0X(?=\d{6})|(0)X(?=\d{5})";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
StringBuffer builder = new StringBuffer();
while (m.find()) {
m.appendReplacement(builder,
"");
}
m.appendTail(builder);
// Print the replaced matcher
System.out.println("After Replacement: "
+ builder.toString());
}
}
但是当我将同一段代码合并到下面的代码中时,它会抛出错误。 (此代码将原始数据写入 CSV 文件,但在写入之前进行十六进制清理。)
public static String writeToCSV(StringBuilder rawData, String granularity, String threadName, long collectionTime)
throws IOException {
StringBuilder fileName = new StringBuilder();
fileName.append(String.valueOf(System.currentTimeMillis())).append("_").append(threadName).append(".csv");
StringBuilder directoryPath = new StringBuilder();
directoryPath.append(TEMP_DATA_DIR_PATH).append(granularity).append(File.separator).append(collectionTime);
AmpStatsDataUtil.createWritableDirectory(directoryPath.toString());
StringBuilder filePath = new StringBuilder();
filePath.append(directoryPath.toString()).append(File.separator).append(fileName.toString());
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath.toString(), true), "UTF-8"));
// Hexadecimal clean up
Pattern r = Pattern.compile("0x(?=\d{6})|(0)x(?=\d{5})");
Matcher m = r.matcher(rawData);
StringBuffer builder = new StringBuffer();
while (m.find()) {
m.appendReplacement(builder,"");
}
m.appendTail(builder);
LOGGER.info("Cleaned hexadecimal digits are {0}",builder.toString());
try {
bw.write(builder.toString());
} catch (IOException e) {
LOGGER.error("Error while writing data to CSV file:{0}", e);
} finally {
try {
if (bw != null) {
bw.flush();
}
bw.close();
} catch (IOException e) {
LOGGER.error("Error while closing bufferedwriter:{0}", e);
}
}
return fileName.toString();
}
但不使用字符串生成器它工作正常:
//code 3
public static String writeToCSV(StringBuilder rawData, String granularity, String threadName, long collectionTime)
throws IOException {
StringBuilder fileName = new StringBuilder();
fileName.append(String.valueOf(System.currentTimeMillis())).append("_").append(threadName).append(".csv");
StringBuilder directoryPath = new StringBuilder();
directoryPath.append(TEMP_DATA_DIR_PATH).append(granularity).append(File.separator).append(collectionTime);
AmpStatsDataUtil.createWritableDirectory(directoryPath.toString());
StringBuilder filePath = new StringBuilder();
filePath.append(directoryPath.toString()).append(File.separator).append(fileName.toString());
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath.toString(), true), "UTF-8"));
// Hexadecimal clean up
Pattern myPattern = Pattern.compile("0x");
Matcher myMatcher = myPattern.matcher(rawData);
try {
bw.write(myMatcher.repalaceAll("0");
} catch (IOException e) {
LOGGER.error("Error while writing data to CSV file:{0}", e);
} finally {
try {
if (bw != null) {
bw.flush();
}
bw.close();
} catch (IOException e) {
LOGGER.error("Error while closing bufferedwriter:{0}", e);
}
}
return fileName.toString();
}
你能告诉我为什么第二段代码不能编译吗?
从Javadoc for Matcher
可以看出appendReplacement
和appendTail
方法只使用了StringBuffer
:
public Matcher appendReplacement(StringBuffer sb, String replacement)
public StringBuffer appendTail(StringBuffer sb)
因此,如果要编译,则需要使用 StringBuffer
版本。