我有一个文本文件,其中包含一个字符串,我正在为我的进一步目的解析文件,我想通过在特定字符串之后向一个字符添加单引号来解析,该怎么做??
Text file data:
{Name:{ID:12342,age:32},type:s},{Name:{ID:12345,age:42},type:t},{Name:{ID:12348,age:35},type:s},{Name:{ID:12349,age:55},type:t}
在这里,我想在字体后面添加单引号:''
Expected o/p:
{Name:{ID:12342,age:32},type:'s'},{Name:{ID:12345,age:42},type:'t'},{Name: {ID:12348,age:35},type:'s'},{Name:{ID:12349,age:55},type:'t'}
我的java代码:
BufferedReader br = new BufferedReader(new FileReader("D:/Workspace/JAVA/Sample/EMP.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String value = sb.toString();
答案 0 :(得分:1)
您可以使用以下string.replceAll
功能。
string.replaceAll("(?<=:)([a-zA-Z]+)", "'$1'");
这会在冒号旁边的单词( only letters )周围添加单引号。
答案 1 :(得分:0)