Algorithm to search and replace delimited parameters

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.
Are there ellipses between the parameters or are they back to back? #param1#...#param2#...#param3# or #param1##param2##param3?

以上就是Algorithm to search and replace delimited parameters 的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏