Skip to content

Commit 4a8793e

Browse files
committed
Added pattern string replacement example
1 parent 511ce51 commit 4a8793e

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

src/main/java/nl/rdb/java_examples/string/StringExample.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package nl.rdb.java_examples.string;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
38
import lombok.extern.slf4j.Slf4j;
49
import nl.rdb.java_examples.scanner.Example;
510

611
@Slf4j
712
public class StringExample {
813

14+
private static final Pattern PATTERN = Pattern.compile("\\{([^}]+)}");
15+
916
@Example
1017
void generateExample() {
1118
log.info(new CodeGenerator().getCode());
@@ -31,4 +38,66 @@ void replace() {
3138
log.info("{}", test.replace(".", "Test-"));
3239
log.info("{}", test.replaceAll("\\.", "Test-"));
3340
}
41+
42+
@Example
43+
void replacePlaceHolders() {
44+
String urlTemplate = "http://localhost/test/{id}/test/{id2}";
45+
46+
Map<String, String> params = new HashMap<>();
47+
params.put("id", "12345");
48+
params.put("id2", "user-profile");
49+
50+
log.info("Original URL Template: {}", urlTemplate);
51+
log.info("User Variables: {}", params);
52+
53+
try {
54+
String finalUrl = replace(urlTemplate, params);
55+
log.info("Final URL: {}", finalUrl);
56+
} catch (IllegalArgumentException e) {
57+
// Log the error with the exception message.
58+
log.error("Error replacing placeholders: {}", e.getMessage());
59+
}
60+
}
61+
62+
@Example
63+
void replacePlaceHoldersMissingPlaceholder() {
64+
String urlTemplate = "http://localhost/test/{id}/test/{id2}";
65+
66+
Map<String, String> params = new HashMap<>();
67+
params.put("id", "12345");
68+
params.put("id2", "user-profile");
69+
70+
Map<String, String> incompleteVariables = new HashMap<>();
71+
incompleteVariables.put("id", "987");
72+
73+
log.info("Original URL Template: {}", urlTemplate);
74+
log.info("Incomplete Variables: {}", incompleteVariables);
75+
76+
try {
77+
String finalUrl = replace(urlTemplate, incompleteVariables);
78+
log.info("Final URL: {}", finalUrl);
79+
} catch (IllegalArgumentException e) {
80+
// It's good practice to also log the exception object itself for a full stack trace
81+
log.error("Error replacing placeholders: {}", e.getMessage(), e);
82+
}
83+
}
84+
85+
private String replace(String template, Map<String, String> variables) {
86+
Matcher matcher = PATTERN.matcher(template);
87+
StringBuilder resultBuilder = new StringBuilder();
88+
89+
while (matcher.find()) {
90+
String placeholderName = matcher.group(1);
91+
String replacement = variables.get(placeholderName);
92+
93+
if (replacement == null) {
94+
throw new IllegalArgumentException("No value found for placeholder: %s".formatted(matcher.group(0)));
95+
}
96+
97+
matcher.appendReplacement(resultBuilder, replacement);
98+
}
99+
100+
matcher.appendTail(resultBuilder);
101+
return resultBuilder.toString();
102+
}
34103
}

0 commit comments

Comments
 (0)