0% found this document useful (0 votes)
7 views23 pages

Regex

The document is a lab exercise on Regular Expressions (regex) for a CS 2112 course, detailing the basics of regex, its implementation in Java, and various concepts such as concatenation, alternation, quantifiers, character classes, and predefined character classes. It also includes examples of regex usage in Java, including the String class and the java.util.regex package, along with exercises for practice. The document emphasizes the utility of regex in pattern matching and input validation.

Uploaded by

f20221335
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

Regex

The document is a lab exercise on Regular Expressions (regex) for a CS 2112 course, detailing the basics of regex, its implementation in Java, and various concepts such as concatenation, alternation, quantifiers, character classes, and predefined character classes. It also includes examples of regex usage in Java, including the String class and the java.util.regex package, along with exercises for practice. The document emphasizes the utility of regex in pattern matching and input validation.

Uploaded by

f20221335
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Regex Basics Basic Patterns Java Exercise

Credit: Randall Munroe xkcd.com

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

CS 2112 Lab 7: Regular Expressions

October 21 / 23, 2019

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Regex Overview

I Regular Expressions, also known as ‘regex’ or ‘regexps’ are a


common scheme for pattern matching in strings
I A regular expression is represented as a single string and
defines a set of matching strings
I The set of strings matched by a regex is the language of the
regular expression.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Regex implementations

I Java supports Perl-style regular expressions through


java.util.regex
I The easyIO package provided with the course also supports
regular expressions.
I Regex terminology is incredibly variable from source to source,
almost everything presented here has other names in certain
contexts.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

The simplest regex

I The simplest regular expression is just a string


I The regex CS2112 matches only the string “CS2112”
I We can add special characters to add more power.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Concatenation and Alternation

I The concatenation AB of two regular expressions A and B


matches all strings with a first part matched by A followed by
a second part matched by B.
I Regex ab is really just the concatenation of a and b.
I The alternation A|B of regexes A and B matches any string
that is matched by either A or B.
I Regex hello|goodbye matches both hello and goodbye.
I Regex d(aa|bb)c matches both daac and dbbc.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Quantifiers

I ab matches only the string ab


I (ab) matches only the string ab (parentheses just do
grouping)
I (ab)* matches any number of ab’s, including the empty
string: “”, “ab”, “abab”, etc.
I Precedence: ab* matches an a followed by any number of b’s:
“a”, “ab”, “abb”, etc.
I (ab)+ matches one or more ab’s. (Same as ab(ab)*)
I (ab)? matches “ab” or the empty string. (Same as ab|)
I 0{3,5} matches 000, 0000, or 00000

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Character classes

I Character classes specify a set of characters to match against:


syntactic sugar for alternation.
I [1] is a trivial class that behaves just like “1”.
I [01] matches 0 or 1 (but not both: same as 0|1)
I [01]{2} matches 00, 11, 01, or 10
I Ranges let you match sets of consecutive characters without
typing them all out:
I [a-z] matches any lowercase letter, [a-z]+ any lowercase
word.
I [0-9] matches any digit.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Combinations

I Character classes and Quantifiers mix to give useful


expressions
I [a-z]* matches any number of consecutive lowercase
characters
I [0-9]+ matches all numbers
I [0-9]{3} matches all three digit numbers
I [A-z]{4} matches all four letter words

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Negation

I The ˆ character beginning a character class is the logical


negation operator
I [^0] matches any character but 0
I [^abc] matches any character but abc
I [^a-z] matches any character but lowercase letters

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Predefined Character classes

I Predefined character classes are shorthand for commonly used


character classes
I In most cases the capital letter is the negation of the lowercase
I \d = [0123456789], \D = [^0123456789]
I \s matches white space (\t, \n, \r, etc.)
I \w matches “word” characters, basically not whitespace and
punctuation.
I . matches anything but a newline. This is super useful.
I There are a lot of these, fortunately the internet knows all of
them!

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Groups

I Groups allow a section of the expression to be remembered for


later
I \n matches the substring captured by the nth capture group.
I (\d):\1 matches 1:1 or 7:7 but not 2:3
I (0|1) matches 0 or 1
I (0|1):\1 matches 1:1 or 0:0 but not 0:1
I (10) matches the string 10 but not 1 or 0 alone
I We’ll see later that groups can be captured and extracted to
do something useful after matching.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Escapes

I regex uses the standard escape sequences like \n, \t, \\


I Characters normally used in quantifiers and groups must also
be escaped
I This includes \+ \( \. \^ among others.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Examples

I Multiple combinations start to get at the real power of regex


I [A-z][0-9] matches things like A1, B6, q0, etc.
I [A-Z][a-z]* [A-z][a-z]* matches a properly capitalized
first and last name (unless you have a name like O’Brian or
McNeil)
I java\.util\.[^(Scanner)].* matches things disallowed on
A3.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Exercise

Write a regex to match Cornell netIDs.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Java.lang.String

The easiest way to start using regular expressions in Java is


through methods provided by the String class. Two examples are
”String.split(String)” and ”String.replaceAll(String,String)”.
1 String TAs = " Reese & Matt & Clara & Chin " ;
2
3 String [] arr = TAs . split ( " & " );
4 for ( String s : arr ){ System . out . println ( s );}
5
6 System . out . println ( TAs . replaceAll ( " &[^&]+ " , " & Reese " ));

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Java.util.regex

I More powerful operations are unlocked by the


Java.util.regex package.
I There are two main classes in this package Pattern and
Matcher
I Pattern objects represent regex patterns have a method to
return a Matcher that allows the pattern to be used.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Java.util.regex.Pattern

I The Pattern object has no public constructor and instead


has a compile method that returns a Pattern object.
I The Java specific version of regular expressions is documented
on the Pattern api page, and is well worth reading.
I Note that you must escape your backslashes when coding
literals

1 Pattern p1 = Pattern . compile ( " [a - z ]{2 ,3}\\ d + " );

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Java.util.regex.Matcher

I Matcher does the actual matching work, as the name


suggests. Again there is no constructor, but instead a method
inside Pattern that allows you to get a Matcher object set to
match on a specific string.
I The principal operations of the Matcher are matches and
find. matches returns true if the entire string matches the
pattern, find returns true if any part of the string matches
the pattern
I Matcher also has methods for operations such as replacement
or group capturing.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Input checking

1 public boolean isUpperLevelCS ( String course ){


2 Pattern p = Pattern . compile ( " CS [456]\\ d {3} " );
3 Matcher m = p . matcher ( course );
4 return m . matches ();
5 }

This example isn’t very powerful, what else can we do?

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Capture example

Here is another example this time used to capture a match:


1 Pattern p1 = Pattern . compile ( " ([ a - z ]{2 ,3}\\ d +) @ .+ " );
2 Matcher m = p1 . matcher ( " rpg55@cornell . edu " );
3 m . matches ();
4 System . out . println ( " First group : " + m . group (1));

This starts to get at the real utility of regex, but this rabbit hole
goes much deeper than we have time for.

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Exercise: Regex Crossword

https://regexcrossword.com/

CS 2112 Lab 7: Regular Expressions


Regex Basics Basic Patterns Java Exercise

Challenge: Command line parsing

I Regex can be used to parse command line or console inputs,


capturing can be used to grab the different tags and access
them
I Write a calculator using regex that takes commands of the
form:
num num -f or num -f num or -f num num
Where num represents a positive decimal number (with or
without a decimal point) and -f is the operation flag, one of
-+ -- -* -/ or -%.
I Parse the input and then print the result of the math.
Implement it as a console (or GUI) application, because
command line parses whitespace.

CS 2112 Lab 7: Regular Expressions

You might also like