1 / 11

Regular Expressions! They’re way cooler than If statements!

Regular Expressions! They’re way cooler than If statements!. Regular Expressions – An Overview. Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set.

dawson
Download Presentation

Regular Expressions! They’re way cooler than If statements!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expressions! They’re way cooler than If statements!

  2. Regular Expressions – An Overview • Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. • They can be used to search, edit, or manipulate text and data. You must learn a specific syntax to create regular expressions; it is different then the Java syntax • Regular expressions vary in complexity, but once you understand the basics of how they're constructed, you'll be able to decipher (or create) any regular expression.

  3. public final class Pattern implements java.io.Serializable • A Pattern object is a compiled representation of a regular expression. • The Pattern class provides no public constructors. • To create a pattern, you must first invoke one of its public static compile methods, which will then return a Pattern object. • These methods accept a regular expression as the first argument • The arguments understand a syntax you will learn later.

  4. public final class Matcher implements MatchResult • A Matcher object is the engine that interprets the pattern and performs match operations against an input string. • Like the Pattern class, Matcher defines no public constructors. • You obtain a Matcher object by invoking the matcher method on a Pattern object.

  5. public class PatternSyntaxException extends IllegalArgumentException • A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern. • Now we are going to teach you how the syntax works :)

  6. Syntax • The metacharacters supported by this API are: ([{\^-$|]})?*+. • Yeah, that’s what I said… • Let’s start simple.

  7. [ ] • Matches one of the characters in the brackets • Examples • (“[abc]”) means “a” or “b” or “c” • (“[hc]at”) matches "hat" and "cat".

  8. [^ ] • This matches anything not in the brackets

  9. $ • Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.

  10. Stuff • ? - Matches the preceding element zero or one time. For example, ba? matches "b" or "ba". • + - Matches the preceding element one or more times. For example, ba+ matches "ba", "baa", "baaa", and so on. • | - The choice (aka alternation or set union) operator matches either the expression before or the expression after the operator. For example, abc|def matches "abc" or "def".

  11. Specials • Quantifiers MeaningGreedyReluctantPossessive • X? X?? X?+ X, once or not at all   • X*  X*?  X*+  X, zero or more times   • X+  X+?  X++  X, one or more times   • X{n}  X{n}?  X{n}+  X, exactly n times   • X{n,}  X{n,}?  X{n,}+  X, at least n times   • X{n,m}  X{n,m}?  X{n,m}+  X, at least n but not more than m times

More Related