Gadgets

Saturday, 27 February 2016

Regular Expression interview questions

If we represent a group of string objects according to particular patter is called “Regular Expression”. By using Regular Expression we can develop validations and pattern matching etc. Here I have given interview questions on regular expression with examples.
1) What is the Regular Expression?
If we represent a group of string objects according to particular patter is called “Regular Expression”.
import java.util.regex.*;
public class RegularExpres  {
                    public static void main(String[] args)  {
                                         int count=0;
                                         Pattern p=Pattern.compile("a");
                                         Matcher m=p.matcher("abcdabc");
                                         while(m.find()) {
                                                             count++;
                                         System.out.println(m.start()+" "+m.end()+" "+m.group());
                                         }
System.out.println("total : " +count);
                    }
}

Output:
      0     1   a
4     5   a
Total : 2
2) What is the pattern object?
Pattern is a class, we can create pattern object by using compile() method of Pattern class. We can define match string.
Pattern p=Pattern.compile("a");

3) What is the Matcher object?
Matcher class object is created by matcher() method of Pattern class. Matcher is used to match the given pattern in the target string.
Methods in Matcher class:
1.      boolean find(): to find next match.
2.      int start(): it return the start index of the match.
3.      int end(): it return the end index of the match.
4.      String group(): it return the matched pattern.

4) Write Regular Expression for email validation?
[a-zA-Z0-9][a-zA-Z0-9]*@[a-zA-Z0-9]+([.][a-zA-Z]+)+
For gmail:  a-zA-Z0-9][a-zA-Z0-9]*@gmail[.]com
Note: a – exactly one ‘a’
a+ - atleast one ‘a’
a* any number of ‘a’ including zero.
a? atmost one ‘a’(one or zero)

5) Write a Regular Expression for mobile number validation?
Only for 10 digits:
[7-9]- series 7 or 8 or 9 and [0-9] remaining numbers.
[7-9][0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]
Or [7-9][0-9]{9}
For 11 digits:
0?[7-9][0-9]{9}  here 0 is code.
For 12 digits:
(0|19)?[7-9][0-9]{9}  here 0 or 19 is code.

6) What is the purpose of split()?
We can use split() method to split the target string according to a particular pattern.
import java.util.regex.*;
public class RegularExpres {
                    public static void main(String[] args) {
                                         int count=0;
                                         Pattern p=Pattern.compile("\\s");
                                         String[] s=p.split("java tech hub");
                                         for(String s1:s)           {                                                           
                                         System.out.println(s1);
                                         }
                    }
}
OUTPUT:
java
tech
hub
7) What is the StringTokenizer?
StringTokenizer is available in java.util package and it is specially designed class for tokenization activity, space character is default regular expression for StringTokenizer.
                        StringTokenizer st=new StringTokenizer("java tech hub");                          
                                         while(st.hasMoreTokens())     {                                                            
                                         System.out.println(st.nextToken());
                                         }
OUTPUT:
java
tech
hub
8) What is the difference between Patter class split() and String class split()?
Both classes(String, Pattern) split() methods are used to split the target string according to a particular pattern.
But String class split() method contain pattern or regular expression as argument.
String s="java tech hub";
String[] s1=s.split("\\.);
Whereas Pattern class split() method contain target string as argument.
Pattern p=Pattern.compile("\\s");
String[] s=p.split("java tech hub");


No comments:

Post a Comment