Question

In: Computer Science

RegEx (Regular Expressions) Make 8 regular expressions to filter a specific data set in Java and...

RegEx (Regular Expressions)

Make 8 regular expressions to filter a specific data set in Java and explain what they do.

Solutions

Expert Solution

1.The . (dot) represents a single character example

System.out.println(Pattern.matches(".s", "as")); //returnstrue because 2nd char is 's'  
System.out.println(Pattern.matches(".s", "amms")); //returns false because it has more than 2 char  
System.out.println(Pattern.matches("..s", "mas")); //returns true because 3rd char is 's'

2.Regular Expression Character classes Example

System.out.println(Pattern.matches("[amn]", "abcd")); //returns false because (not a or m or n)
System.out.println(Pattern.matches("[amn]", "a")); //returns true because (among a or m or n)

Regular Expression Character classes and Quantifiers Example

3. ? quantifier  
System.out.println(Pattern.matches("[amn]?", "a")); //returns true as (a or m or n comes one time)
System.out.println(Pattern.matches("[amn]?", "aaa")); //returns false as (a comes more than one time)

4. + quantifier
System.out.println(Pattern.matches("[amn]+", "aammmnn")); //returns true as (a or m or n comes more than once)
System.out.println(Pattern.matches("[amn]+", "aazzta")); //returns false as (z and t are not matching pattern)
  
5. * quantifier
System.out.println(Pattern.matches("[amn]*", "ammmna"));//returns true (a or m or n may come zero or more times)

6. Regular Expression Metacharacters Example
  
System.out.println(Pattern.matches("\\d", "abc"));//return false as (non-digit)
System.out.println(Pattern.matches("\\d", "1"));//returns true as (digit and comes once)
System.out.println(Pattern.matches("\\d", "4443"));//returns false as (digit but comes more than once)

7. System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));//returns true

8. // Following line prints "true" because the whole
// text "hacksforhacks" matches pattern "hacksforha*ks"
System.out.println (Pattern.matches("hacksforha*ks", "hacksforhacks"));

Sample program -

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

public class RegexMatches {

   private static String REGEX = "dog";
   private static String INPUT = "The dog says meow. " + "All dogs say meow.";
   private static String REPLACE = "cat";

   public static void main(String[] args) {
      Pattern p = Pattern.compile(REGEX);
      
      // get a matcher object
      Matcher m = p.matcher(INPUT); 
      INPUT = m.replaceAll(REPLACE);
      System.out.println(INPUT);
   }
}

Output

The cat says meow. All cats say meow.

Related Solutions

Understanding RegEx to identify patterns of data. Create 10 regular expressions to filter a specific data...
Understanding RegEx to identify patterns of data. Create 10 regular expressions to filter a specific data set and explain what they each do.
Understanding RegEx to identify patterns of data. 1. Create 4 regular expressions to filter a specific...
Understanding RegEx to identify patterns of data. 1. Create 4 regular expressions to filter a specific data set. 2. In addition to the description, provide two test cases that will pass the input and one that will fail
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided 1.Show only the lines that end with an ellipses (3 dots) :
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use “re” or “Regex” to denote regular expressions. Write the Python Code to return matching analytics word in the following given text. Write the Python Code to return how many times analytics word is provided in this text. Definitions are useful to the extent they serve a purpose. So, is defining analytics important? Yes and no. It’s not likely that we’ll ever arrive at a...
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
XYZ wants to have $192,000 in 8 years. He plants to make regular savings contributions of...
XYZ wants to have $192,000 in 8 years. He plants to make regular savings contributions of $9,400 per year for 8 years, with the first of these regular savings contributions made later today. In addition, XYZ expects to make a special savings contribution of X in 2 years. He expects to earn 13.6 percent per year. What is X, the amount fo the special savings contribution that XYZ will make in 2 years?
Is there a way to make a pivot table from a data set to show the...
Is there a way to make a pivot table from a data set to show the following: - make gender the columns (one column for male and one for female) - rows are age increments (18 - 30, 31 - 40, 41 - 50, 51 - 60, 61 - 70) - information provided within the pivot table is the average salary of everyone within the age increment (for example, I want to find the average salary of a male between...
QUESTION Write the main while loop for a Java program that processes a set of data...
QUESTION Write the main while loop for a Java program that processes a set of data as follows: Each line of the input consists of 2 numbers representing a quantity and price. Your loop should: 1. Read in the input 2. Calculate the tax. Tax will be 8.875% of price and will only be applicable if price is more than 110. Calculate the new price which is the price plus tax. 3. Calculate the final price by multiplying quantity by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT