1

I am using the maven replacer plugin and I've run into a situation where I have a regular expression that matches across lines which I need to run on the input file until all matches have been replaced. The configuration for this expression looks like this:

<regexFlags>
    <regexFlag>DOTALL</regexFlag>
</regexFlags>
<replacements>   
    <replacement>
        <token>\@([^\n\r=\@]+)\@=([^\n\r]*)(.*)(\@default\.\1\@=[^\n\r]*)(.*)</token>
        <value>@$1@=$2$3$5</value>
    <replacement>
<replacements>

The input could look like this:

@d.e.f@=y
@a.b.c@=x
@h.i.j@=aaaa
@default.a.b.c@=QQQ
@asdfasd.fasdfs.asdfa@=23423
@default.h.i.j@=234
@default.RR.TT@=393993

and I want the output to look like this:

@d.e.f@=y
@a.b.c@=x
@h.i.j@=aaaa

@asdfasd.fasdfs.asdfa@=23423

@default.RR.TT@=393993

The intention is to re-write the file, but without the tokens with a @default prefix, where another token without the prefix has already been defined.

@default.a.b.c@=QQQ and @default.h.i.j@=234 have been removed from the output because other tokens already contains a.b.c and h.i.j.

The current problem I have is that the replacer plugin only replaces the first match, so my output looks like this:

@d.e.f@=y
@a.b.c@=x
@h.i.j@=aaaa

@asdfasd.fasdfs.asdfa@=23423
@default.h.i.j@=234
@default.RR.TT@=393993

Here, @default.a.b.c=QQQ is gone, which is correct, but @default.h.i.j@=234 is still present.

If I were writing this in code, I think I could probably just loop while attempting to match on the entire output, and break when there are no matches. Is there a way to do this with the replacer plugin?


Edit: I may have over simplified my example. A more realistic one is:

@d.e.f@=y
@a.b.c@=x
@h.i.j@=aaaa
@default.a.b.c@=QQQ
@asdfasd.fasdfs.asdfa@=23423
@default.h.i.j@=234
@default.RR.TT@=393993
@x.y.z@=0
@default.q.r.s@=1
@l.m.n@=8.3
@q.r.s@=78
@blah.blah.blah@=blah

This shows that it's possible for a default.x.x.x=y to precede a x.x.x=y token (as @default.q.r.s@=1 preceedes @q.r.s@=78`), my prior example wasn't clear about this. I do actually have an expression to capture this, it looks a bit like this:

\@default\.([^\n\r=@|]+)@=([^\n\r|]*)(.*)@\1@=([^\n\r|]*)(.*)

I know line separators are missing from this even though they were in the other one - I was experimenting with removing all line separators and treating it as a single line but that hasn't helped. I can resolve this problem simply by running each replacement multiple times by copying and pasting the configurations a few times, but that is not a good solution and will fail eventually.

4

2 回答 2

1

我不相信您可以按原样解决此问题,解决方法是将文件从上到下的顺序颠倒,执行前瞻正则表达式,然后颠倒结果顺序
模式 = @default\.(.*?)@[^\r\n]+(?=[\s\S]*@\1@) Demo

另一种方式(取决于“Maven”的能力)是运行这个模式

@(.*)(@[\s\S]*)@default\.\1.*  

并在循环中替换为@$1$2 Demo直到没有匹配项

然后运行这个模式

@default\.(.*)@.*(?=[\s\S]*\1)  

并在循环中替换为 nothing Demo直到没有匹配项

于 2013-12-13T05:06:27.380 回答
0

看起来替换插件实际上不能做我想做的事。我通过使用正则表达式构建多个过滤器文件,然后将它们应用于资源文件来解决这个问题。

我最初的目标是使用正则表达式来构建一个单一、干净、整洁的过滤器文件。最后,我发现我能够摆脱只使用多个过滤器(不是干净或整洁)并以正确的顺序应用它们。

于 2013-12-18T19:15:34.213 回答