Question

In: Computer Science

Java:    Find a pattern that will match any string that is --  at least 6 characters...

Java:

   Find a pattern that will match any string that is

--  at least 6 characters long,

-- and begins with a letter or number (\w)

-- and contains at least one non-letter and non-number (\W).

Solutions

Expert Solution

Question

Java:

   Find a pattern that will match any string that is

--  at least 6 characters long,

-- and begins with a letter or number (w)

-- and contains at least one non-letter and non-number (W).

Program :

import java.util.regex.*;
import java.util.Scanner;
class RegexExample1{
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string : ");
      String content = sc.next();

      String pattern = "(^w(?=.*W).{6,})";

      boolean isMatch = Pattern.matches(pattern, content);
      System.out.println("Given string matched to the regular expression" + isMatch);
   }
}

OUTPUT :


Related Solutions

Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
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
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available...
Find a valid tax court case to match the fact pattern evident in the case presented...
Find a valid tax court case to match the fact pattern evident in the case presented below: CPA Joe reimburses a client for a $75,000 tax liability that is traceable to Joe’s ineffective tax advice. For fear of increasing his already steep malpractice-insurance premiums, Joe does not file a claim with the insurer. Can Joe deduct the $75,000 loss? Facts in this case: Joe reimburses a client $75,000 due to his ineffective tax advice and he does not file a...
use java recursion find # of times a substring is in a string but it has...
use java recursion find # of times a substring is in a string but it has to be the exact same, no extra letters attached to it and capitalization matters. input is 2 strings, output is an int input: ("Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "hello") should output: 3 input: (" Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "Hello") should output: 1 input: (" Hello...
use java recursion find # of times a substring is in a string but it has...
use java recursion find # of times a substring is in a string but it has to be the exact same, no extra letters attached to it and capitalization matters. input: school is boring with schooling and School substring: school output: 1
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
A DNA strand is represented by a string of the characters A, C, G, and T,...
A DNA strand is represented by a string of the characters A, C, G, and T, each of which represents a nucleotide. Each nucleotide has its complement as indicated by this Ruby hash: NUCLEOTIDE_COMPLEMENT = { 'A' => 'T', 'T' => 'A', 'C' => 'G', 'G' => 'C' } The reverse-complement of a DNA string is a new string in which each nucleotide is replaced by its complement and the string is reversed. Reverse-complements are important in bioinformatics since a...
This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT