Java Assignment- 9

Regular Expressions

Basic Questions

  1. Write a Java program that uses Pattern and Matcher to check if a string exactly matches “JAVA”. Print true or false.
  2. Write a program that checks whether a string contains only digits (one or more) using the pattern \\d+. Test with “12345” and “12a45”.
  3. Write a program that finds and prints all vowels in the text “Regular Expressions in Java” using a character class like [aeiouAEIOU].
  4. Write a program that matches a single lowercase letter at the start of a string using ^[a-z] and prints whether it matches for “java” and “Java”.
  5. Write a program that matches any three characters using … (dot dot dot) and prints whether “abc” and “ab” match.
  6. Write a program that matches a word made of letters or digits using \\w+ and prints the first match from “id=U123 code”. Print the matched word.
  7. Write a program that finds all spaces in a string using \\s and prints how many were found.
  8. Write a program that uses a character set to find every digit or letter A from the string “A1 B2 C3” with the regex [A\\d]. Print each match.
  9. Write a program that validates a string that has exactly 4 digits using ^\\d{4}$. Test with “1200” and “120”.
  10. Write a program that uses the + quantifier to check if a string has one or more a characters: pattern ^a+$. Test with “aaa” and “”.
  11. Write a program that uses the ? quantifier to match “color” or “colour” using colou?r. Print whether each matches.
  12. Write a program that uses the * quantifier to check if a line starts with “ha” followed by zero or more ! characters: ^ha!*. Test with “ha”, “ha!!!”.
  13. Write a program that uses anchors to verify that a string ends with “.txt” using \\.txt$. Test with “notes.txt” and “notes.txt.bak”.
  14. Write a program that extracts the first number from “Item-42 costs 199 rupees” using \\d+ and prints it.
  15. Write a program that splits a sentence on one or more spaces using String.split(“\\s+”) and prints each token on a new line.
  16. Write a program that replaces all digits in “Room 507 on 3rd floor” with # using replaceAll(“\\d”, “#”). Print the result.
  17. Write a program that is case-insensitive when matching “java” by compiling the pattern with Pattern.CASE_INSENSITIVE. Test “JAVA” and “Java”.
  18. Write a program that escapes a literal . in a filename by using Pattern.quote(“.”) to split “a.b.c” into [“a”,”b”,”c”]. Print the parts.
  19. Write a program that uses a group to capture the area code and the rest of the number from “011-2345678” using (\\d+)-(\\d+) and prints both groups.
  20. Write a short comment in code explaining the difference between find() and matches(), then demonstrate both on the text “abc123xyz” with the pattern \\d+.

Intermediate Questions

  1. Write a program that extracts day, month, year from “12-08-2025” using groups and the pattern ^(\\d{2})-(\\d{2})-(\\d{4})$. Print each captured part.
  2. Write a program that validates a simple username: 3–15 characters, letters/digits/underscore only. Use ^[A-Za-z0-9_]{3,15}$ and test a few examples.
  3. Write a program that validates a pin code of exactly 6 digits (India) using ^\\d{6}$. Test with “282001” and “2820A1”.
  4. Write a program that extracts all email-like tokens from a sentence using a basic pattern [A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,} and prints each.
  5. Write a program that validates a simple email using the same pattern as above with matches(). Print valid or invalid for test inputs.
  6. Write a program that validates a phone number of the form “9876543210” (10 digits) using ^\\d{10}$. Print valid or invalid.
  7. Write a program that validates a phone number with country code like +91-9876543210 using ^\\+\\d{1,3}-\\d{10}$. Test with two examples.
  8. Write a program that extracts a price value from “Total: INR 1,250.50 only” using a pattern like \\d{1,3}(,\\d{3})*(\\.\\d{2})? and prints the match.
  9. Write a program that matches time in 24-hour format HH:MM using ^([01]\\d|2[0-3]):[0-5]\\d$. Test with “09:45”, “24:15”.
  10. Write a program that validates date format YYYY-MM-DD using ^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$. Test with sample inputs.
  11. Write a program that uses alternation to match “cat” or “dog” or “bird”: \\b(cat|dog|bird)\\b. Print all matches in “my cat and dog”.
  12. Write a program that uses a negated character class [^0-9] to remove everything that is not a digit from a string. Print the cleaned numeric string.
  13. Write a program that uses a backreference to find double words like “is is” using \\b(\\w+)\\s+\\1\\b. Print all matches in a test sentence.
  14. Write a program that uses capturing groups to reorder “last, first” into “first last” with replaceAll(“(\\w+),\\s*(\\w+)”, “$2 $1”). Print the result.
  15. Write a program that finds all file extensions in a list like “a.txt b.java c.TXT” using \\.([A-Za-z0-9]+) and prints just the extension group.
  16. Write a program that splits a CSV line such as “A,\”B,C\”,D” using a simple regex that respects basic quoted fields (hint pattern: “,(?=(?:[^\”]*\”[^\”]*\”)*[^\”]*$)”). Print tokens.
  17. Write a program that demonstrates greedy vs reluctant quantifiers by matching the shortest and longest quoted text in “He said \”hi\” and \”hello\”” using \”.*\” and \”.*?\”. Print both matches.
  18. Write a program that compiles a precompiled Pattern as a static final field and reuses it to validate many inputs. Add a comment on why precompiling helps.
  19. Write a program that shows how to escape a backslash in Java strings when writing regex (e.g., write \\d in Java to mean \d in regex). Print a simple example.
  20. Write a program that reads a regex and a test string from the console, applies the pattern with find() in a loop, and prints each match with its start and end index.

Advanced Questions

  1. Write a robust email validator method using a stricter pattern that disallows spaces and requires a dot in the domain. Test with valid and invalid cases and print reasons in simple messages.
  2. Write a phone number normalizer that accepts “+91 98765-43210”, “9876543210”, and “(+91)9876543210″ and outputs a standard form ” +91-9876543210 ” using capturing groups and replaceAll().
  3. Write a date extractor that finds all dates in formats DD/MM/YYYY, DD-MM-YYYY, and YYYY-MM-DD using alternation and groups. Print each date and also print which format matched.
  4. Write a key–value parser for lines like name=”Alice” age=30 city=’Agra’. Use groups to capture key and value (accept single/double quotes or bare numbers/words). Print each pair.
  5. Write a log line parser that extracts timestamp, log level, and message from
    [2025-08-27 10:15:30] INFO – Server started
    using groups. Print the three parts.
  6. Write a method that validates strong passwords: length ≥ 8, at least one uppercase, lowercase, digit, and special character from @#$%^&+=!. Use a single regex with lookaheads or (if you prefer) multiple regex checks, then print strong/weak.
  7. Write a method that uses named capturing groups (e.g., (?<user>[A-Za-z]+)@(?<host>[^\\s@]+)) to split an email into user and host. Print user= and host= from Matcher.group(“user”) and Matcher.group(“host”).
  8. Write a URL extractor for http and https links using a pattern like https?://[^\\s\”‘]+. Print each URL and its starting index.
  9. Write a file mask converter that converts a simple wildcard like “*.txt” to a regex ^.*\\.txt$ using Pattern.quote and small replacements, then uses it to list matching names from an array of demo filenames.
  10. Write a small regex test harness that:
    • Reads a pattern string and a test text from the console,
    • Compiles with optional flags: case-insensitive ((?i)), multiline ((?m)), or dotall ((?s)) if the user prefixes the pattern with those inline flags,
    • Prints all matches with their indices,
    • Prints a note suggesting that students can also try the same pattern in any online regex tester for quick experimentation.