0

In a properties file I'm loading a String containing (with delimiters and a Regular expression) method names I want to dynamically invoke from Java.

For example : "%getProductName%-%GetProductCode%[-]*[0-9]*\.(.+)"

When the String get's read from the properties file, I want to replace the method names (between %%) with their return value when the method is called.

But I'm not quite sure how to accomplish following in Java (best practice): - Parse string & retrieve all the %methodNames% variables.

After retrieving the different methods defined in the String, I will retrieve the result using Reflection and replacing this value in the original String.

I'm just looking for a quick approach regarding parsing the String and retrieving the different %methodNames%.

Thanks for any advice!

daan.desmedt
  • 3,752
  • 1
  • 19
  • 33

1 Answers1

0

You could try regex like this :

public static void main(String[] args) {
    String s = "%getProductName%-%GetProductCode%[-]*[0-9]*\\.(.+)";
    Pattern p = Pattern.compile("%(.*?)%"); // capturing group to extract data between ()s
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }

}

O/P :

getProductName
GetProductCode
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • I was answering the *I'm just looking for a quick approach regarding parsing the String and retrieving the different %methodNames%.* part. @downvoters, please explain yourselves. – TheLostMind Jan 12 '15 at 14:40