Regular Expressions Interview Questions and Answers
Question - 41 : - What is an example of Java Regular Expressions?
Answer - 41 : -
Here is an example of Java Regular Expression:
import java.util.regex.*;
public class RegexExample1{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();
//2nd way
boolean b2=Pattern.compile(".s").matcher("as").matches();
//3rd way
boolean b3 = Pattern.matches(".s", "as");
System.out.println(b+" "+b2+" "+b3);
}}
Question - 42 : - Write a regex to split String by new line?
Answer - 42 : -
String lines[] = string.split("\\r?\\n");
Question - 43 : - How can we split Java String by newlines?
Answer - 43 : -
There are many ways for entering a new line character:
\r represents CR (Carriage Return), which is used in Unix
\n means LF (Line Feed), used in Mac OS
\r\n means CR + LF, used in Windows
The most forward way for splitting strings by new line is :
String lines[] = String.split("\\r?\\n");
Question - 44 : - What is a metacharacter?
Answer - 44 : -
Metacharacter acts as a special meaning to a regular expression engine.It is not counted as a regular character by the regex engine.
Question - 45 : - What is Flags?
Answer - 45 : -
Flags in java are the method for changing the search when it is performed.Here are some of the options of flag:
- Pattern.CASE_INSENSITIVE - The case of letters will be ignored when performing a search.
- Pattern.LITERAL - Special characters in the pattern will not have any special meaning and will be treated as ordinary characters when performing a search.
- Pattern.UNICODE_CASE - Use it together with the CASE_INSENSITIVE flag to also ignore the case of letters outside of the English alphabet.
Question - 46 : - What is Capturing Groups?
Answer - 46 : -
Capturing Groups are used for treating multiple characters as a single unit, it is created by placing the characters to be grouped inside a set of parentheses.
There are 4 types of such groups:
((A)(B(C)))
(A)
(B(C))
(C)
Question - 47 : - How to validate an email address in JavaScript?
Answer - 47 : -
Here is an example for validating an email address in JavaScript:
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function validate() {
const $result = $("#result");
const email = $("#email").val();
$result.text("");
if (validateEmail(email)) {
$result.text(email + " is valid :)");
$result.css("color", "green");
} else {
$result.text(email + " is not valid :(");
$result.css("color", "red");
}
return false;
}
$("#validate").on("click", validate);
Question - 48 : - How to negate specific word in regex?
Answer - 48 : -
We can negate word in regex by using the following code:
^(?!.*bar).*$
The negative lookahead construct is the pair of parentheses.
Question - 49 : - How to count string occurrence in string?
Answer - 49 : -
var temp = "This is a string.";
var count = (temp.match(/is/g) || []).length;
console.log(count);
Question - 50 : - How can we replace multiple spaces with a single space?
Answer - 50 : -
We can cover tabs, newlines by using the following code:
string = string.replace(/\s\s+/g, ' ');
We can cover only spaces by using the following code:
string = string.replace(/ +/g, ' ');