我正在尝试过滤某些文件的内容,该文件的内容可由我String
访问:
block "block1"
{
ADDSFDJF
SDFSDFSDF
SDFSDFSDF
// subblock: subblock1 [master]
include "/path/tofile/subblock1.conf";
+/- subblock subblock1
// subblock: subblock1
subblock "subblock1"
{
type TYPE;
file "name.file";
details blah blah.
other {sdhsdf};
};
};
file "dddd.file";
details blah blah.
// subblock: subblock2
include "/path/tofile/subblock2.conf";
+/- subblock subblock2
// subblock: subblock2
subblock "subblock2"
{
type TYPE;
file "name.file";
details blah blah.
other {sdhsdf};
more fields
};
};
这里我想删除每个子块的一些行,即:我想删除这些行:
+/- subblock subblock1
// subblock: subblock1
subblock "subblock1"
{
type TYPE;
file "name.file";
details blah blah.
other {sdhsdf};
};
};
我在尝试content.replaceALL();
但是我无法创建与regex
相匹配的正确'+/-'
,以便开始和正确关闭'}'它的括号。
请帮我解决这个问题。
[编辑] :正则表达式作为Java字符串
答案 0 :(得分:0)
你可能能够相处:
Java
请参阅a demo on regex101.com
注意:您必须双倍转义var express = require('express');
var app = express();
function setHeaders(res, path, stat){
res.append('Access-Control-Allow-Origin', '*');
res.append('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
}
app.use('/dist', express.static(__dirname + '/dist', {setHeaders: setHeaders}));
/*app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});*/
var port = process.env.PORT || 8080;
/*(app.get('*', function(req, res, next) {
console.log(req.get('Access-Control-Allow-Origin'));
res.sendFile(__dirname + '/dist/index.html');
});*/
app.listen(port);
console.log('listen at ' + port);
中的反斜杠。
答案 1 :(得分:0)
我通过正则表达式实现了这一目标:
(?m)\\+\\/\\-[\\s\\S]+?//[\\s\\S]+?(^\\};\\s+$)
作为java字符串。或
\+\/\-[\s\S]+?//[\s\S]+?(^\};\s+$)
作为python正则表达式。
这里,Pattern.MULTILINE或(?m)告诉Java接受锚点^和$在每行的开头和结尾匹配(否则它们只匹配整个字符串的开头/结尾)。 参考:https://stackoverflow.com/a/3652392/5580576
见: Demo on regex101.com !!