Class Notes (807,180)
Canada (492,652)
CSC207H1 (40)


2 Pages
Unlock Document

University of Toronto St. George
Computer Science
Diane Horton

NOVEMBER 05  Regular Expressions o General Idea  A regular expression is a pattern that a string may or may not match.  Example: [0-9]+  [0-9] means a character in that range  “+” means one or more of what came before  Strings that match: 9125 4  Strings that don’t: -45 empty string  Symbols like * and + have special meaning. They are not part of the string that matches.  (If we want them to be, we escape them with a backslash.) o Uses  It’s much easier to declare a pattern that you want matched than to  Therefore mayn languages offer support fort his  Bonus: By having the pattern explicitly declared, rather than implicit in code that matches it, it’s much easier to  Understand what the pattern is  Modify it o Example Uses  Handling white space  A program ought to be able to treat any number of white space characters as a separator  Identifying blank lines  Most people consider a line with just spaces on it to be blank  Writing code to examine characters one by one is painful! Regular expressions make it easy. o Patterns o Escaping: to actually match a character use \  \t is a tab character, \n is a new line o Character sets  [^abc] matches anything except for the characters “a”, “b”, or “c” o Other Patterns NOVEMBER 05  Regular Expressions in Java o the java.util.regex package contains  Pattern: a compiled regular expression  Matcher: the result of a match o 1. package RegexDemo; 2. 3. import; 4. import; 5. import; 6. import java.util.regex.Matcher; 7. import java.util.regex.Pattern; 8. 9. public class Main { 10. public static void doMatching() { 11. BufferedReader in = new BufferedReader(new InputStreamReader(; 12. try { 13. String pattern, s; 14. System.out.print("Pattern: "); 15. pattern = in.readLine(); 16. while (!pattern.equals("quit")) { 17. System.out.print("String: "); 18. s = in.readLine(); 19. System.out.println(Pattern.matches(pattern, s)); 20.
More Less

Related notes for CSC207H1

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.