【发布时间】:2021-07-16 16:50:57
【问题描述】:
尝试创建一个函数,将内容附加到文件中与某个正则表达式条件匹配的行
AccountFile.txt
1 {
2 "account": "123456",
3 "MoreAccounts": { //Some-Comment-1
4 "evenMoreAccounts: {
5 "Hello" : "World"
6 }
7 }
8 }
所以如果要匹配的正则表达式是.*Some-Comment-1,它将匹配第三行。
例如说我想插入文本
"anotherAccount" : {
"HelloWorld" : "2"
},
在与上面的正则表达式匹配的行下,我的程序的输出应该是:
1 {
2 "account": "123456",
3 "MoreAccounts": { //Some-Comment-1
4 "anotherAccount" : {
5 "HelloWorld" : "2"
6 },
7 "evenMoreAccounts: {
8 "Hello" : "World"
9 }
10 }
11 }
我面临的问题是格式,如何确保我要插入的文本始终与任何文件扩展名 .py, .yaml, .txt, .json, .java 的周围文本格式匹配
现在我的函数正在输出类似这样的东西
1 {
2 "account": "123456",
3 "MoreAccounts": { //Some-Comment-1
4 "anotherAccount" : {
5 "HelloWorld" : "2"
6 },
7 "evenMoreAccounts: {
8 "Hello" : "World"
9 }
10 }
11 }
是否有任何好的外部库可以为我做到这一点?感谢所有帮助
或者即使是可以美化任何文本文件的 Java 库也会很有用
我用来写这个的编程语言是Java,所以java函数或库会很有帮助
我调查过的事情
- JAlopy ~ 仅适用于 java
【问题讨论】:
-
您可以通过将文件读入
List行然后在匹配时进行插入来编造一些东西。不过,这看起来有点像 json - 或者这只是偶然的?
标签: java file formatter fileparsing