LearnJavaInterview Questions(RegularExpression Problem)

                                                                                  


REGULAR EXPRESSION PROBLEM

AddDashBeforeAndAfterEveryVowel

public class AddDashBeforeAndAfterEveryVowel {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         String text = "C++";
            System.out.println("Original string: "+text);
            System.out.println("Add a dash before and after every vowel in the said
string: "+validate(text));
            text = "Java";
            System.out.println("\nOriginal string: "+text);
            System.out.println("Add a dash before and after every vowel in the said
string: "+validate(text));
            text = "MID-CENTRALIZED";
            System.out.println("\nOriginal string: "+text);
            System.out.println("Add a dash before and after every vowel in the said
string: "+validate(text));
            text = "LOWERED";
            System.out.println("\nOriginal string: "+text);
            System.out.println("Add a dash before and after every vowel in the said
string: "+validate(text));

    }
    public static String validate(String txt) {
        return txt.replaceAll("([AEIOUaeiou])","-$1-");
    }

}

AddDashBetweenUpperAndLowerCaseLetter

public class AddDashBetweenUpperAndLowerCaseLetter {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

         String text = "Python Exercises";
            System.out.println("Original string: "+text);
            System.out.println("Insert a dash between an upper case letter and a
lower case letter in the said string:\n"+validate(text));
            text = "The quick brown Fox jumps over the lazy Dog.";
            System.out.println("\nOriginal string: "+text);
            System.out.println("Insert a dash between an upper case letter and a
lower case letter in the said string:\n"+validate(text));  
            text = "java exercises";
            System.out.println("\nOriginal string: "+text);
            System.out.println("Insert a dash between an upper case letter and a
lower case letter in the said string:\n"+validate(text));
            }
public static String validate(String txt) {
    return txt.replaceAll("(?<=[A-Z])(?=[a-z])","-");
}
}

CheckAWordContainingTheCharacterGInAGivenString

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckAWordContainingTheCharacterGInAGivenString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(validate("obulipurusothaman"));
        System.out.println(validate("gopalchettiyar"));
        System.out.println(validate("gokul"));
    }
    public static  String validate(String txt) {
        Pattern pattern=Pattern.compile("\\w*g.\\w*");
        Matcher m=pattern.matcher(txt);
        if(m.find()) {
            return "Match found";
        }
        else {
            return "Not found";
        }
    }

}

CheckForANumberAtTheEndOfAString

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckForANumberAtTheEndOfAString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(validate("19CS094"));
        System.out.println(validate("OBULIPURUSOTHAMAN"));
        System.out.println(validate("01.102.103.104"));
    }
public static String validate(String txt) {
    Pattern pattern=Pattern.compile(".*[0-9]$");
    Matcher m=pattern.matcher(txt);
    if(m.find())
        return "Match found";
    else
        return "Not found";
}
}

CheckTheStringContainsOnlyACertainSetOfCharacters

public class CheckTheStringContainsOnlyACertainSetOfCharacters {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    System.out.println(validate("obuli"));
    System.out.println(validate("19CS094"));
    System.out.println(validate("*()&^%$#@"));
    System.out.println(validate("936171"));
    System.out.println(validate("google.com"));
    }
    public static boolean validate(String txt) {
        return txt.matches("^[\\w]+$");
    }

}

CheckTheStringStartsWithASpecificNumbersOrNot

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckTheStringStartsWithASpecificNumbersOrNot {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(validate("592838383"));
        System.out.println(validate("5-12345-1923738"));
        System.out.println(validate("19cs094"));
        System.out.println(validate("637412"));
    }
    public static String validate(String txt) {
        Pattern pattern=Pattern.compile("^5.*$");
        Matcher m=pattern.matcher(txt);
        if(m.find())
            return "Match found";
        else
            return "Not Found";
    }

}

CountNoOfVowelsInAString

import java.util.*;
public class CountNoOfVowelsInAString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
String a="C++";
System.out.println("the original string is:"+a);
System.out.println("no of vowels is:"+validate(a));
 a="java";
System.out.println("the original string is:"+a);
System.out.println("no of vowels is:"+validate(a));
 a="python";
System.out.println("the original string is:"+a);
System.out.println("no of vowels is:"+validate(a));
 a="obulipurusothaman";
System.out.println("the original string is:"+a);
System.out.println("no of vowels is:"+validate(a));
 a="JAVA";
System.out.println("the original string is:"+a);
System.out.println("no of vowels is:"+validate(a));
 a="OBULIPURUSOTHAMAN";
System.out.println("the original string is:"+a);
System.out.println("no of vowels is:"+validate(a));
    }
    public static int validate(String txt) {
        return txt.replaceAll("[^aeiouAEIOU]","").length();
    }

}

CountTheNumberOfDecimalPlacesInAGivenNumber

public class CountTheNumberOfDecimalPlacesInAGivenNumber {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String a="122";
        System.out.println("the original string is:"+a);
        System.out.println("decimal count is:"+validate(a));
         a="128.0447";
        System.out.println("the original string is:"+a);
        System.out.println("decimal count is:"+validate(a));
         a="111.111";
        System.out.println("the original string is:"+a);
        System.out.println("decimal count is:"+validate(a));
         a="303";
        System.out.println("the original string is:"+a);
        System.out.println("decimal count is:"+validate(a));
         a="40.403";
        System.out.println("the original string is:"+a);
        System.out.println("decimal count is:"+validate(a));
         a="122";
        System.out.println("the original string is:"+a);
        System.out.println("decimal count is:"+validate(a));

    }
    public static Integer validate(String txt) {
        if(txt.contains("."))
        return txt.replaceAll(".*\\.(?=\\d?)", "").length();
        return 0;
    }

}

FindAStringThatHasAPFollowedByAnythingEndingInQ

public class FindAStringThatHasAPFollowedByAnythingEndingInQ {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         System.out.println(validate("papaya"));
         System.out.println(validate("pppqqqq"));
         System.out.println(validate("pqqqqqqqqqqqq"));
         System.out.println(validate("pq"));
    }
    public static String validate(String txt) {
        if(txt.matches("p.*?q$")) {
            return "Matches Found";
        }
        else {
            return "No Matches";
        }
    }

}

FindSequenceOfOneUpperCaseLetterFollowedByLowerCaseLetters

public class FindSequenceOfOneUpperCaseLetterFollowedByLowerCaseLetters {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         System.out.println(validate("obuli"));
         System.out.println(validate("Obuli"));
         System.out.println(validate("OBULI"));
         System.out.println(validate("OB"));
         System.out.println(validate("oB"));
         System.out.println(validate("Ob"));
    }
    public static String validate(String txt) {
        if(txt.matches("[A-Z][a-z]+$")) {
            return "Matches Found";
        }
        else {
            return "Not Matches";
        }
    }

}

FindTheSequenceOfLowerCaseLettersJoinedWithAUnderscore

public class FindTheSequenceOfLowerCaseLettersJoinedWithAUnderscore {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         System.out.println(validate("Java_Programs"));
         System.out.println(validate("java_exercises"));
         System.out.println(validate("JAVA_EXCERISE"));
         System.out.println(validate("java_examples"));
         System.out.println(validate("JAVA_EXAMPLES"));
    }
    public static String validate(String txt) {
        if(txt.matches("^[a-z]+_[a-z]+$")) {
            return "Found a match";
        }
        else {
            return "Not found";
        }
    }

}

GivenStringIsValidHexCodeOrNot

public class GivenStringIsValidHexCodeOrNot {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String text = "123456";
        System.out.println("Original String: "+text);
        System.out.println("Check the said string is a valid hex code or not? "
+validate(text));
        text = "#eaecff";
        System.out.println("\nOriginal String: "+text);
        System.out.println("Check the said string is a valid hex code or not? "
+validate(text));
        text = "#FF0000";
        System.out.println("\nOriginal String: "+text);
        System.out.println("Check the said string is a valid hex code or not? "
+validate(text));
        text = "#DD5C5C";
        System.out.println("\nOriginal String: "+text);
        System.out.println("Check the said string is a valid hex code or not? "
+validate(text));
        text = "#0000000";
        System.out.println("\nOriginal String: "+text);
        System.out.println("Check the said string is a valid hex code or not? "
+validate(text));        
    }
    public static Boolean validate(String txt) {
        return txt.matches("#[0-9A-Fa-f]{6}");
    }

}

MatchAStringThatContainsOnlyUpperAndLowerCaseLttersNumbersAndUnderscores

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatchAStringThatContainsOnlyUpperAndLowerCaseLttersNumbersAndUnderscores{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(validate("obulipurusothaman"));
        System.out.println(validate("OBULIPURUSOTHAMAN"));
        System.out.println(validate("19CS094"));

    }
public static String validate(String txt) {
    Pattern pattern=Pattern.compile("^[a-zA-Z0-9]*$");
    Matcher m=pattern.matcher(txt);
    if(m.find())
        return "Match Found";
    else
        return "Not Found";
}
}

MatchesAStringThatHasAPFollowedByZeroOrMoreQs

public class MatchesAStringThatHasAPFollowedByZeroOrMoreQs {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         System.out.println(validate("p"));
         System.out.println(validate("pq"));
         System.out.println(validate("pqqq"));
         System.out.println(validate("pqr"));
         System.out.println(validate("psg"));
    }
    public static String validate(String a) {
        if(a.matches("pq*?")){
            return "Found a match";
                    }
        else {
            return "Not matched";
        }
    }

}

MoveAllTheLowerCaseToFront

public class MoveAllTheLowerCaseToFront {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
String txt="Java";
System.out.println("the original string is:"+txt);
System.out.println("Move all lower case letters to the front of the said word: "
+validate(txt));
txt="JavaScript";
System.out.println("the original string is:"+txt);
System.out.println("Move all lower case letters to the front of the said word: "
+validate(txt));
txt="SqLite";
System.out.println("the original string is:"+txt);
System.out.println("Move all lower case letters to the front of the said word: "
+validate(txt));
    }
    public static String validate(String txt) {
        return txt.replaceAll("[A-Z]","")+txt.replaceAll("[a-z]","");
    }

}

RemoveAllTheVowelsOfAGivenStringAndReturnTheNewString

public class RemoveAllTheVowelsOfAGivenStringAndReturnTheNewString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String a="C++";
        System.out.println("the original string is:"+a);
        System.out.println("no of vowels is:"+validate(a));
         a="java";
        System.out.println("the original string is:"+a);
        System.out.println("no of vowels is:"+validate(a));
         a="python";
        System.out.println("the original string is:"+a);
        System.out.println("no of vowels is:"+validate(a));
         a="obulipurusothaman";
        System.out.println("the original string is:"+a);
        System.out.println("no of vowels is:"+validate(a));
         a="JAVA";
        System.out.println("the original string is:"+a);
        System.out.println("no of vowels is:"+validate(a));
         a="OBULIPURUSOTHAMAN";
        System.out.println("the original string is:"+a);
        System.out.println("no of vowels is:"+validate(a));
            }
            public static String validate(String txt) {
                return txt.replaceAll("(?i)[aeiou]","");
            }

}

RemoveLeadingZerosFromAGivenIPAddress

public class RemoveLeadingZerosFromAGivenIPAddress {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(validate("216.08.094.196"));
        System.out.println(validate("08.008.0008.00008"));
        System.out.println(validate("01.102.103.104"));
    }
    public static String validate(String txt) {
        return txt.replaceAll("(?<=^|\\.)0+(?!\\.|$)","");
    }

}

ReplaceAllTheVowelsInAGivenStringWithASpecifiedCharacter

 import java.util.*;
public class ReplaceAllTheVowelsInAGivenStringWithASpecifiedCharacter {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String a="C++";
        System.out.println("the original string is:"+a);
        System.out.println("replace vowels is:"+validate(a,'*'));
         a="java";
        System.out.println("the original string is:"+a);
        System.out.println("replace vowels is:"+validate(a,'@'));
         a="python";
        System.out.println("the original string is:"+a);
        System.out.println("replace vowels is:"+validate(a,'#'));
         a="obulipurusothaman";
        System.out.println("the original string is:"+a);
        System.out.println("replace vowels is:"+validate(a,'@'));
         a="JAVA";
        System.out.println("the original string is:"+a);
        System.out.println("replace vowels is:"+validate(a,'#'));
         a="OBULIPURUSOTHAMAN";
        System.out.println("the original string is:"+a);
        System.out.println("replace vowels is:"+validate(a,'*'));
            }
    public static String validate(String txt,char c) {
        return txt.replaceAll("[aeiouAEIOU]",""+ c);
    }

}

ReplacePythonWithJavaAndCodeWithCodingInJava

public class ReplacePythonWithJavaAndCodeWithCodingInJava {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String text = "Python is an interpreted high-level general-purpose programming
language. " +
                "Its design philosophy emphasizes code readability with its use of
significant indentation. " +
                "Its language constructs as well as its object-oriented approach
aim to help programmers write" +
                "clear, logical code for small and large-scale projects.";
System.out.println("the orginal string is:"+text);
System.out.println("the replaced string is:"+validate(text));
    }
    public static String validate(String txt) {
        return txt.replace("Python","Java").replace("code","coding");
    }

}

ReplaceWordInString

public class ReplaceWordInString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String a="python";
        System.out.println("the orginal string is:"+a);
        System.out.println("the replaced string is:"+validate(a));
        a="JavaScript";
        System.out.println("the orginal string is:"+a);
        System.out.println("the replaced string is:"+validate(a));
        a="PYTHON";
        System.out.println("the orginal string is:"+a);
        System.out.println("the replaced string is:"+validate(a));

    }
public static String validate(String txt) {
    String a=txt.toLowerCase();
    if(a.contains("python"))
        return "Java";
    else
        return "C++";
}
}

SeperateConsonantsAndVowelsFromAGivenString


public class SeperateConsonantsAndVowelsFromAGivenString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String txt="Java";
        System.out.println("the original string is:"+txt);
        System.out.println("Separate consonants and vowels of the said string: "
+validate(txt));
        txt="JavaScript";
        System.out.println("the original string is:"+txt);
        System.out.println("Separate consonants and vowels of the said string: "
+validate(txt));
        txt="SqLite";
        System.out.println("the original string is:"+txt);
        System.out.println("Separate consonants and vowels of the said string: "
+validate(txt));

    }
    public static String validate(String txt) {
        String a=txt.replaceAll("(?i)[^aeiou]","");
        String b=txt.replaceAll("(?i)[aeiou]","");
        return a+b;
    }

}

SetThousandsSeperatorInTheSaidNumber

public class SetThousandsSeperatorInTheSaidNumber {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         int n = 100;
            System.out.println("Original Number: "+n);
            System.out.println("Set thousands separator in the said number): "
+validate(n));
            n = 1000;
            System.out.println("\nOriginal Number: "+n);
            System.out.println("Set thousands separator in the said number): "
+validate(n));
            n = 10000;
            System.out.println("\nOriginal Number: "+n);
            System.out.println("Set thousands separator in the said number): "
+validate(n));    
            n = 100000;
            System.out.println("\nOriginal Number: "+n);
            System.out.println("Set thousands separator in the said number): "
+validate(n));        
            n = 1000000;
            System.out.println("\nOriginal Number: "+n);
            System.out.println("Set thousands separator in the said number): "
+validate(n));
    }
public static String validate(int num) {
    String b=Integer.toString(num);
    int len=b.length();
    if(len<4) {
        return b;
    }
    return validate(Integer.parseInt(b.substring(0, len-3))) + '#' +
b.substring(len-3);
}
}

ValidateAPersonalIdentificationNumberPIN

public class ValidateAPersonalIdentificationNumberPIN {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         String n = "123";
            System.out.println("Original PIN Number: "+n);
            System.out.println("Is the said PIN number is valid? "+validate(n));
            n = "1234";
            System.out.println("Original PIN Number: "+n);
            System.out.println("Is the said PIN number is valid? "+validate(n));
            n = "12345";
            System.out.println("Original PIN Number: "+n);
            System.out.println("Is the said PIN number is valid? "+validate(n));    
            n = "123456";
            System.out.println("Original PIN Number: "+n);
            System.out.println("Is the said PIN number is valid? "+validate(n));
            n = "1234567";
            System.out.println("Original PIN Number: "+n);
            System.out.println("Is the said PIN number is valid? "+validate(n));        
            n = "12345678";
            System.out.println("Original PIN Number: "+n);
            System.out.println("Is the said PIN number is valid? "+validate(n));
            n = "123456789";
            System.out.println("Original PIN Number: "+n);
            System.out.println("Is the said PIN number is valid? "+validate(n));        
    }
    public static boolean validate(String txt) {
        return txt.matches("\\d{4}\\d{6}\\d{8}");
    }

}

ValidatePhoneNumber

public class ValidatePhoneNumber {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         String text ="(123)4567890";
            System.out.println("Original Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "+validate(text));
            text ="(123)4567890";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));
            text ="1234567890";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="123-456-7890";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="(1234567890)";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="123)4567890";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="12345678901";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="(1)234567890";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="(123)-4567890";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="1";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));      
            text ="12-3456-7890";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));
            text ="123-4567";
            System.out.println("\nOriginal Phone number: "+text);
            System.out.println("Check the said Phone number is true or not! "
+validate(text));
    }
    public static Boolean validate(String txt) {
        return txt.matches("\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}");
    }

}








Post a Comment

Previous Post Next Post