• +91 9723535972
  • info@interviewmaterial.com

Regular Expressions Interview Questions and Answers

Regular Expressions Interview Questions and Answers

Question - 31 : - What is Name matched sub expressions in Regular expressions?

Answer - 31 : -

Matched string captured by sub expression by using name. Let you access this it by name  or number

syntax : (?  subexpression)

Ex : (? \w+)   -> Capturing method syntax 

Console.WriteLine(Match.Goups["given_name"].value);

Question - 32 : - What is conditional matching in Regex?

Answer - 32 : -

-Using conditional matching  match the text from one of two given patterns.

(?(capturinggroup) pattern1/pattern2)

((?\d{2}-)\b\d{2}\b | \b\d{4}\b)

12-222-9999

Question - 33 : - What is Zero width assertions in Regular expressions?

Answer - 33 : -

These are using for matching the position rather than text the line.

\A - Matching the starting of  the line.

\Z - Matching the end of the line. Etc

Question - 34 : - Write a regex to split String by new line?

Answer - 34 : -

String lines[] = string.split("\\r?\\n");

Question - 35 : - What is use of Dot(.) symbol in Java Regex?

Answer - 35 : -

The dot is used for matching any character. For example, the following regex represents "a number plus any other character":
[0-9].

Question - 36 : - How to extract a substring using regex ?

Answer - 36 : -

Example - String test = "This is a test String and 'This is data we want'"
A:
String data = "This is a test String and 'This is data we want'";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(data);
if (matcher.find())
{
    System.out.println(matcher.group(1));
}

Question - 37 : - What is difference between matches() and find() in Java Regex?

Answer - 37 : -

matches() returns true only if the whole string matches the specified pattern while find() returns trues even if a substring matches the pattern.
import java.util.regex.*;

public class RegexTutorial {
public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\d");
        String test = "JavaInUse123";
        Matcher m = pattern.matcher(test);

        if (m != null){
            System.out.println(m.find());
            System.out.println(m.matches());
        }
    }
}

Question - 38 : - Create a regular expression that accepts alphanumeric characters only. Its length must be five characters long only

Answer - 38 : -

import java.util.regex.*;

public class RegexTutorial {
public static void main(String args[]) {
System.out.println(Pattern.matches("[a-zA-Z0-9]{5}", "java1"));
System.out.println(Pattern.matches("[a-zA-Z0-9]{5}", "java12"));
System.out.println(Pattern.matches("[a-zA-Z0-9]{5}", "JA1Va"));
System.out.println(Pattern.matches("[a-zA-Z0-9]{5}", "Java$"));
}
}  

Question - 39 : -
What are the type of classes in Java?

Answer - 39 : -

There are three types of classes in Java:
  • Pattern Class - Used as a compiled representation of a regular expression.It provides no public constructors.
  • Matcher Class - Used as an engine which interprets the pattern and also performs match operations against an input string.
  • PatternSyntaxException - Used in indicating a syntax error in a regular expression pattern.

Question - 40 : -
What is Matcher class?

Answer - 40 : -

Matcher Class helps in performing match operations on a character sequence.The method description is as follows:
  • boolean matches() used in testing the regular expression which matches the pattern.
  • boolean find() helps in finding the next expression which matcges the pattern.
  • boolean find(int start) used in finding the next expression which matches the pattern from given the start number.
  • String group() helps in returning the matched subsequence.
  • int start() helps in returning the starting index of the matched subsequence.
  • int end() helps in returning the ending index of the matched subsequence.
  • int groupCount() helps in returning the total number of the matched subsequence.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners