1

I have a string that contains multiple parameters delimited by #, like this :

.... #param1# ... #param2# ... #paramN# ...

And I want to replace the parameter placeholders by values.

The current algorithm looks like this:

    //retrieve place holder into this SQL select
    Pattern p = Pattern.compile(DIMConstants.FILE_LINE_ESCAPE_INDICATOR);
    Matcher m = p.matcher(sqlToExec); // get a matcher object
    int count = 0;
    int start = 0;
    int end = 0;

    StringBuilder params = new StringBuilder();
    while (m.find()) {
        count++;

        if (count % 2 == 0) {

            // Second parameter delimiter

            String patternId = sqlToExec.substring(start, m.end());

            //Clean value (#value#->value)
            String columnName = patternId.substring(1, patternId.length() - 1);

            //Look for this column into preLoad row ResultSet and retrieve its value
            String preLoadTableValue = DIMFormatUtil.convertToString(sourceRow.get(columnName));

            if (!StringUtils.isEmpty(preLoadTableValue)) {
                aSQL.append(loadGemaDao.escapeChars(preLoadTableValue).trim());
            } else {
                aSQL.append(DIMConstants.COL_VALUE_NULL);
            }
            params.append(" " + columnName + "=" + preLoadTableValue + " ");

            end = m.end();

        } else {
            // First parameter delimiter
            start = m.start();
            aSQL.append(sqlToExec.substring(end, m.start()));
        }
    }

    if (end < sqlToExec.length()) {
        aSQL.append(sqlToExec.substring(end, sqlToExec.length()));
    }

I'm looking for a simplest solution, using regexp or another public API. Input parameters will be the source string, a delimiter and a map of values. Output parameter will be the source string with all the parameters replaced.

2
  • Are there ellipses between the parameters or are they back to back? #param1#...#param2#...#param3# or #param1##param2##param3? Commented Jan 20, 2010 at 14:57
  • The separators are never back to back. Commented Jan 20, 2010 at 15:14

2 Answers 2

3

If this is for a normal SQL query, you might want to look into using PreparedStatements

Beyond that, am I missing something? Why not just use String.replace()? Your code could look like this:

for(int i = 0; i < n; i++){
   String paramName = "#param" + i + "#"
   sqlToExec = sqlToExec.replace(paramName,values.get(paramName));
}

That assumes you have a map called "values" with string mappings between parameters in the form "#paramN#"

Sign up to request clarification or add additional context in comments.

2 Comments

The name of the parameters is free, but yes I could read all the keys in the map and to the replace.
The queries are retrieved from an XML and using PreparedStatements implies changing the format of the parameters (to use ?).
0

If you need it more generic, this will find and return the whole param including the #'s:

public class ParamFinder {

    public static void main(String[] args) {
        String foo = "#Field1# #Field2# #Field3#";

        Pattern p = Pattern.compile("#.+?#");
        Matcher m = p.matcher(foo);
        List matchesFound = new ArrayList();
        int ndx = 0;
        while(m.find(ndx)){
            matchesFound.add(m.group());
            ndx = m.end();
        }

        for(Object o : matchesFound){
            System.out.println(o);
        }


    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.