Question

In: Computer Science

Write a JAVA code using regex methods and concepts The test string S should match the...

Write a JAVA code using regex methods and concepts The test string S should match the following requirements:

  • S must be of length 6
  • First character should not be a digit (1, 2, 3, 4, 5, 6, 7, 8, 9 or 0).
  • Second character should not be a lowercase vowel (a, e, i, o or u).
  • Third character should not be b, c, D or E.
  • Fourth character should not be a whitespace character (\r, \n, \t, \f or the space character).
  • Fifth character should not be an uppercase vowel (A, E, I, O or U).
  • Sixth character should not be a . or , symbol.

Solutions

Expert Solution

Please upvote if you like the answer, as it helps the community a lot.

Also if you have any doubts, feel free to ask in comments, we will reach you ASAP.

Solution:

CODE: MatchMyPattern.Java

import java.util.regex.Pattern;
import java.util.Scanner;
  
class MatchMyPattern
{
public static void main(String args[])
{
  
   Scanner sc = new Scanner(System.in);
   System.out.print ("Enter Text to be Matched with the pattern: ");
   String S = sc.next();
   System.out.print ("Text Match Result is: ");
System.out.println (Pattern.matches("[^0-9][^aeiou][^bcDE][^\n\r\t\f ][^AEIOU][^.,]",S));
}
}

SAMPLE I/Os:

Enter Text to be Matched with the pattern: SILENT
Text Match Result is: true
Enter Text to be Matched with the pattern: 0ILENT
Text Match Result is: false
Enter Text to be Matched with the pattern: SiLENT
Text Match Result is: false
Enter Text to be Matched with the pattern: SIDENT
Text Match Result is: false
Enter Text to be Matched with the pattern: SIL NT
Text Match Result is: false
Enter Text to be Matched with the pattern: SILEAT
Text Match Result is: false
Enter Text to be Matched with the pattern: SILEN.
Text Match Result is: false
Enter Text to be Matched with the pattern: SILENTS
Text Match Result is: false

Related Solutions

Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
Please use the Java program below to test the following regex pattern (in the table) and...
Please use the Java program below to test the following regex pattern (in the table) and record the output, and explain how the output is generated. import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTestBarness {           public static void main(String[] arg) {               Scanner input = new Scanner(System.in);                 while (true) {                    System.out.println("Enter a regex pattern: ");                    String regex = input.nextLine();               ...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
Write a java code to ask the user for a string, and assuming the user enters...
Write a java code to ask the user for a string, and assuming the user enters a string containing character "a", your java program must convert all "a" to "b" in the string so for example if user types : "AMAZON" , your program must convert it to "BMBZON" the driver program must ask : enter a string containing "a" or "A" --------- (user enters string) then it should say; HERE IS YOUR NEW STRING ---------- (string with all a's...
using java language only write a code Have the function StringChallenge(str) take the str string parameter...
using java language only write a code Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 ------- you have the following code edit it to get the result mport java.util.*; import java.io.*; class Main {   public static String StringChallenge(String str)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT