0

We got a Pattern:

Pattern TRANSLATION_RAW_PATTERN =  Pattern.compile("(?<key>(\\$\\{(?<keydata>.*?)\\}))|(?<value>(.*?))")

And an Input:

${something}${something.else}

And using an online Pattern matcher this should result in the two keydata groups of:

"something" and "something.else"

But, it actually results in "something}${something.else"

    String rawContents = getContents();
    Matcher contentMatcher = TRANSLATION_RAW_PATTERN.matcher(rawContents);

    while (contentMatcher.matches())
    {
        final String keyGroupMatching = contentMatcher.group("keydata");
        if (keyGroupMatching == null)
        {
            break;
        }

        rawContents = rawContents.replace("${" + keyGroupMatching + "}", I18n.translateToLocal(keyGroupMatching));
        contentMatcher = TRANSLATION_RAW_PATTERN.matcher(rawContents);
    }

    return rawContents;

This happens already on the first iteration. The first time it retrieves the group it already retrieves it wrong.

We're a bit puzzled here since all online Pattern matchers we tried return the correct groups.

raycons
  • 735
  • 12
  • 26
  • 1
    `.matches()` requires a full string match. Use `.find()` to find multiple partial matches. Note that `(.*?)` will always match empty string, so you will probably have to adjust that part of the pattern according to your requirements. – Wiktor Stribiżew Sep 02 '18 at 16:39
  • Ty, that solved it. – raycons Sep 02 '18 at 17:01

0 Answers0