Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

9
LINES

< > BotCompany Repo | #1030662 - replaceDollarVars2_dyn - does not use a tokenizer. also supports ${name}

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2735L/16K).

// f takes variable without $ sign. if it returns null, variable is kept
sS replaceDollarVars2_dyn(S s, IF1<S> f) {
  if (f == null) ret s;
  ret regexpReplace(s, "\\$(\\w+)|\\$\\{(.+?)\\}", matcher -> {
    S var = or(matcher.group(1), matcher.group(2));
    S val = f.get(var);
    ret val == null ? matcher.group() : str(val);
  });
}

Author comment

Began life as a copy of #1030433

download  show line numbers  debug dex   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1030662
Snippet name: replaceDollarVars2_dyn - does not use a tokenizer. also supports ${name}
Eternal ID of this version: #1030662/5
Text MD5: 8c9bf832e4e5432e6b782ebf77931dfc
Transpilation MD5: aed7d5fc0803320e39cf8748f96596ab
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-02-22 13:09:43
Source code size: 345 bytes / 9 lines
Pitched / IR pitched: No / No
Views / Downloads: 10 / 26
Version history: 4 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1030662 & http://1030662.tinybrain.de