public static String[] getArgs(final String patternString, final String sourceString, final String[] keywordPrecedence) {

        log.debug("Util getArgs String[] with pattern: " + patternString + " and sourceStr: "
                + sourceString);

        String[] rtn = null;

        ArrayList<String> argsList = null;

        String patternCopy = new String(patternString);
        if (keywordPrecedence != null && StringUtils.startsWithAny(patternString, keywordPrecedence)){
            //
            for (String s : keywordPrecedence){


                patternCopy = StringUtils.removeStart(patternCopy, s);
            }

            patternCopy = "(?:" + StringUtils.join(keywordPrecedence, "|") + ")" + patternCopy;
        }


        final Pattern pattern = Pattern.compile(patternCopy);
        final Matcher matcher = pattern.matcher(sourceString);

        final int groupCount = matcher.groupCount();

        // TODO - this doesn't work if we're not doing strict matching
        if (matcher.find()) {

            for (int i = 1; i <= groupCount; i++) {
                final String arg = matcher.group(i);

                if (arg != null) {
                    if (argsList == null) {
                        argsList = new ArrayList<String>();
                    }
                    argsList.add(arg);
                }
            }
        }

        if (argsList != null) {
            rtn = argsList.toArray(new String[argsList.size()]);

            if (log.isDebugEnabled()) {

                final StringBuilder buf = new StringBuilder();
                buf.append("returning args: ");

                for (final String s : argsList) {

                    buf.append("[").append(s).append("] ");
                }

                log.debug(buf.toString());
            }

        }

        return rtn;
    }
public static String [ ] getArgs ( final String patternString , final String sourceString , final String [ ] keywordPrecedence ) { log . debug ( "Util getArgs String[] with pattern: " + patternString + " and sourceStr: " + sourceString ) ; String [ ] rtn = null ; ArrayList < String > argsList = null ; String patternCopy = new String ( patternString ) ; if ( keywordPrecedence != null && StringUtils . startsWithAny ( patternString , keywordPrecedence ) ) { for ( String s : keywordPrecedence ) { patternCopy = StringUtils . removeStart ( patternCopy , s ) ; } patternCopy = "(?:" + StringUtils . join ( keywordPrecedence , "|" ) + ")" + patternCopy ; } final Pattern pattern = Pattern . compile ( patternCopy ) ; final Matcher matcher = pattern . matcher ( sourceString ) ; final int groupCount = matcher . groupCount ( ) ; if ( matcher . find ( ) ) { for ( int i = 1 ; i <= groupCount ; i ++ ) { final String arg = matcher . group ( i ) ; if ( arg != null ) { if ( argsList == null ) { argsList = new ArrayList < String > ( ) ; } argsList . add ( arg ) ; } } } if ( argsList != null ) { rtn = argsList . toArray ( new String [ argsList . size ( ) ] ) ; if ( log . isDebugEnabled ( ) ) { final StringBuilder buf = new StringBuilder ( ) ; buf . append ( "returning args: " ) ; for ( final String s : argsList ) { buf . append ( "[" ) . append ( s ) . append ( "] " ) ; } log . debug ( buf . toString ( ) ) ; } } return rtn ; }
- could they be combined ??