Search code examples
mvel

MVEL extracting a string from a string


I have the strings "000134567 - AA - 2001" and "002134567 - AB - 2001" and I want to extract all the numbers before the " - AA". But I only want to return the numbers starting from the first non-zero number. For example, I would want "134567" or "2134567".

Is there some function that would allow me to do this using MVEL? Any help is appreciated.


Solution

  • I do not think directly we can use MVEL for this, but yes tweaking little bit might help us.

    import java.io.Serializable;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.mvel2.MVEL;
    import org.mvel2.ParserContext;
    import org.mvel2.integration.VariableResolverFactory;
    import org.mvel2.integration.impl.MapVariableResolverFactory;
    
    public class MyTest {
        public static void main(String[] args) {
            String myString = "000134567 - AA - 2001";
    
            Map contextMap = new HashMap();
            contextMap.put("myString", myString);
    
            ParserContext ctx = new ParserContext();
            ctx.addPackageImport("java.util.regex");
    
            Serializable s = MVEL.compileExpression("outputString(myString)", ctx);
    
            System.out.println(MVEL.executeExpression(s, contextMap, getMvelFactory(contextMap)));
        }
    
        private static VariableResolverFactory getMvelFactory(Map contextMap) {
            VariableResolverFactory functionFactory = new MapVariableResolverFactory(contextMap);
            MVEL.eval(
                    "outputString = def (myString) { java.util.regex.Pattern p = java.util.regex.Pattern.compile(\"[0-9]+\"); java.util.regex.Matcher m = p.matcher(myString); if (m.find()) { String output = m.group(); return output.replaceAll(\"^0+\", \"\");}};",
                    functionFactory);
            return functionFactory;
        }
    }
    

    Function

    Below is my function written in java which i have included inside Mvel VariableResolverFactory.

    outputString = def (myString) { 
            java.util.regex.Pattern p = java.util.regex.Pattern.compile("[0-9]+");
            Matcher m = p.matcher(myString);
            if (m.find()) {
                String output = m.group();
                return output.replaceAll("^0+", "");
            }
     };
    

    Creating Parser Context and adding import

    Since java.util.regex package is not available with MVEL, so we will import this package explicitly.

        ParserContext ctx = new ParserContext();
        ctx.addPackageImport("java.util.regex");
    

    Compiling Expression using above Import

    This will just compile the expression and will add imports for later use while evaluation.

    Serializable s = MVEL.compileExpression("outputString(myString)", ctx);
    

    Varying variable

    You can pass through your variables as below,

        Map contextMap = new HashMap();
        contextMap.put("myString", myString);
    

    Rest you can change the function body, signature and return statement as per your needs.