1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. public class MetadataTokenizer {
  6. public static List<String> tokenizeMetadata(String metadata) {
  7. List<String> tokens = new ArrayList<>();
  8. // Define regular expressions for different metadata elements
  9. String[] patterns = {
  10. "name=(?<name>[\\w\\s]+)", // Name: captures alphanumeric characters and spaces
  11. "description=(?<description>[\\w\\s\\.,;:'\"]+)", // Description: alphanumeric, spaces, punctuation
  12. "author=(?<author>[\\w\\s]+)", // Author: alphanumeric and spaces
  13. "date=(?<date>[\\d]{4}-[\\d]{2}-[\\d]{2})", // Date: YYYY-MM-DD format
  14. "version=(?<version>[\\d.]+)", // Version: digits and dots
  15. "tags=(?<tags>[\\w\\s,-]+)" // Tags: alphanumeric, spaces, and commas
  16. };
  17. // Iterate through the patterns
  18. for (String pattern : patterns) {
  19. Pattern regex = Pattern.compile(pattern);
  20. Matcher matcher = regex.matcher(metadata);
  21. while (matcher.find()) {
  22. tokens.add(matcher.group()); // Add the matched group to the tokens
  23. }
  24. }
  25. return tokens;
  26. }
  27. public static void main(String[] args) {
  28. String metadata = "Name=My Application\n" +
  29. "Description=This is a sample application, for testing.\n" +
  30. "Author=John Doe\n" +
  31. "Date=2023-10-27\n" +
  32. "Version=1.2.3\n" +
  33. "Tags=sample, testing, automation";
  34. List<String> tokens = tokenizeMetadata(metadata);
  35. for (String token : tokens) {
  36. System.out.println(token);
  37. }
  38. }
  39. }

Add your comment