Regular Expressions (Pre-Finals)

Cards (46)

  • Regular Expressions or Regex is a character or a sequence of characters that present a string or multiple strings.
  • Regular Expressions are part of java.util.regex package
  • java.util.regex package; this package has classes to help form and use regular expressions.
  • Regular expressions allow for quicker, easier searching, parsing, and replacing of characters in a string.
  • Regular expressions are used to ensure that strings contain specific contents and are often used to check correct email format (@ sign is included) on form validation.
  • The String class contains a method named matches(String regex) that returns true if a string matches the given regular expression same as String method equals(String str).
  • comparing a string to a regular expression allows variability.
  • The equals() method compares one (1) string to another by comparing a single argument.
  • The matches() method allows you to determine what the string should match against and allows you to specify multiple values.
  • matches() method allows much more flexibility in your code.
  • The regular expression symbol | allows for the method matches to check if
    animal is equal to "cat" or "dog" and return true accordingly.
  • Square brackets are used in regular expressions to allow for character variability.
  • For example, you wanted to return true if animal is equal to "dog" or "Dog", but not "dOg". Using equalsIgnoreCase would not work, and using equals would take multiple lines. If you use regular expression, this task can be done in a single statement:
    return animal.matches("[Cc]at|[Dd]og");
  • Square brackets are not restricted to two-character options.
    They can be combined with a hyphen to include any range of characters.
  • Regular expressions make it much easier to solve similar problems. A shorter, more generic way to complete the same task is to use square brackets and a hyphen (regular expression),
  • The dot (.) is the wildcard operator that represents any single character in regular expressions. For example, you are to write code that checks if a String element consists of a number followed by any other single character. Your statement would look like this:
    return element.matches("[0-9].");
  • The dot (.) is the wildcard operator that represents any single character in regular expressions.
  • A repetition operator is any symbol in regular expressions that indicates the number of times a specified character appears in a matching string.
  • ".*" means any number of any characters in a sequence will return true.
     '
  • The Pattern class in the java.util.regex package stores the format of a regular expression.
  • The compile method compiles the given regular expression into a pattern.
  • A compiled regex pattern can speed up your program when the pattern is used frequently.
  • The Matcher class in the java.util.regex package stores a possible match between a pattern and a string.
  • The matcher method returns a Matcher object. Use the matches() method to check if the regular expression given in the Pattern declaration matches a given string.
  • Matcher's find() method scans the input sequence looking for the next subsequence that matches the pattern. It will return true if the defined pattern exists as a substring of the string of the matcher.
  • The split() method splits the string around matches of the given regular expression. It is used to specify where the operator wishes to split the string.
  • The replaceAll() method is used to replace all the occurrences of the defined regular expression found in the string with another string.
  • Meaning of Repetition Operator *
  • Meaning of Repetition Operator ?
  • Meaning of Repetition Operator +
  • Meaning of Repetition Operator {x}
  • Meaning of Repetition Operator {x,y}
  • Meaning of Repetition Operator {x,}
  • Returns true if str is a sequence of 5 or more A's.

    {x,}
  • return str.matches("A{5,}");
    Returns true if str is a sequence of 5 or more A's.
  • Returns true if str is a sequence of 7, 8, or 9 A's
    return str.matches("A{7,9}");
  • return str.matches("A{7}");

    Returns true if str is a sequence of 7 A's
  • Returns true if str is 1 or more A's in a sequence
    return str.matches("A+");
  • return str.matches("A?");
    Returns true if str is "" or "A"
  • Returns true if str consists of zero or more A's but no other letter
    return str.matches("A*");