Created
July 8, 2024 14:21
-
-
Save muhdkhokhar/127a01415aa89f5df78c86f050e9e9a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class PlaceholderExtractor { | |
public static void main(String[] args) { | |
String input = "abc %current_uk_time% test %another_one%"; | |
// Extract placeholders from the input string | |
Set<String> placeholders = extractPlaceholders(input); | |
// Print extracted placeholders | |
System.out.println(placeholders); // Output: [current_uk_time, another_one] | |
} | |
public static Set<String> extractPlaceholders(String input) { | |
Set<String> placeholders = new HashSet<>(); | |
// Pattern to match placeholders in the format %placeholder_name% | |
Pattern pattern = Pattern.compile("%(.*?)%"); | |
Matcher matcher = pattern.matcher(input); | |
while (matcher.find()) { | |
// Extract the placeholder name (without the % characters) | |
String placeholder = matcher.group(1); | |
placeholders.add(placeholder); | |
} | |
return placeholders; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment