I am trying to create some sort of translation egine. I am given a mapping of attributes, and using this mapping I need to translate an expression.
For example, I have this mapping:
{
"a": "A",
"b[*]": "B[*]",
"c": "C",
"ac": "D"
}
And an example input might be (the rule is that each token can be followed by a ., it's actually a JSONPath):
a.b[*] should translate to A.B[*]
And each input can appear arbitraritly in the expression, for example I can have a mix:
a.c.b[*] => A.C.B[*]
My solution was to create a list of Regexes out of this mapping, and looping that, searching and replacing the expression for each regex.
The problem was in inputs like that:
ac => should have translated to D but instead, since there exist mapping for a and c, their regexes match and I get AC instead.
I thought to use word boundaries \b, but it didn't work well in cases there were special chars, like in the b[*] example, as it's not included in the word boundary.
I also thought to extend the boundary, but nothing worked as expected.
In the bottom line: is there a way to replace a stirng by another, considering that only a full match is valid, but an expression can be part of a JSONPath string?