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.