📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!

  • google app store apple app store
  • ✖

      Question

      Consider the following Java code: import

      java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexGroup {     public static void main(String[] args) {         String text = "Date: 2023-10-26, Time: 14:30";         Pattern pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");         Matcher matcher = pattern.matcher(text);         if (matcher.find()) {             System.out.println(matcher.group(0) + " | " + matcher.group(1));         } else {             System.out.println("No match");         }     } } What will be the output of this program?
      A 2023-10-26 | 2023 Correct Answer Incorrect Answer
      B 2023-10-26 | 10 Correct Answer Incorrect Answer
      C 2023-10-26 | 26 Correct Answer Incorrect Answer
      D No match Correct Answer Incorrect Answer
      E 2023 | 10 Correct Answer Incorrect Answer

      Solution

      Correct Answer: A (matcher.group(0) returns the entire matched string. matcher.group(1) returns the content of the first capturing group, which is (\d{4}).)

      Practice Next
      ask-question