CSC207 Week 9. Larry Zhang

Similar documents
Peer Instruction 1. Elementary Programming

Programming Languages and Techniques (CIS120)

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Monday, August 26, 13. Scanners

Wednesday, September 3, 14. Scanners

York University Department of Electrical Engineering and Computer Science. Regular Expressions

Program Elements -- Introduction

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

What did we talk about last time? Examples switch statements

Programming Languages and Techniques (CIS120)

Using Java Classes Fall 2018 Margaret Reid-Miller

CS11 Java. Fall Lecture 1

Chapter 2: Basic Elements of Java

More Examples. Lex/Flex/JLex

Dining philosophers (cont)

Lecture 11.1 I/O Streams

Computer Systems and Architecture

Full file at

Programming Languages and Techniques (CIS120)

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Chapter 2: Data and Expressions

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Full file at

CS 251 Intermediate Programming Java I/O Streams

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Introduction to regular expressions

Software Practice 1 Basic Grammar

Introduction to Programming

Regexs with DFA and Parse Trees. CS230 Tutorial 11

Form Validation (with jquery, HTML5, and CSS) MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

CIS192 Python Programming

CSC 467 Lecture 3: Regular Expressions

Lecture Set 2: Starting Java

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Computational Expression

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lecture Set 2: Starting Java

ECE251 Midterm practice questions, Fall 2010

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Object Oriented Software Design

Maciej Sobieraj. Lecture 1

Object Oriented Software Design

Scanners. Xiaokang Qiu Purdue University. August 24, ECE 468 Adapted from Kulkarni 2012

Basic Computation. Chapter 2

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Dr. Sarah Abraham University of Texas at Austin Computer Science Department. Regular Expressions. Elements of Graphics CS324e Spring 2017

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Programming Languages and Techniques (CIS120)

Lesson 1. Introduction to Programming OBJECTIVES

Today, we will dispel the notion that Java is a magical language that allows us to solve any problem we want if we re smart enough.

HST 952. Computing for Biomedical Scientists Lecture 8

First Java Program - Output to the Screen

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

The Wildcard Side of SAS

Week 2: Data and Output

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Repetition Using the End of File Condition

JAVA BYTE IPC: PART 1 DERIVING NIO FROM I/O. Instructor: Prasun Dewan (FB 150,

Regular Expressions. Steve Renals (based on original notes by Ewan Klein) ICL 12 October Outline Overview of REs REs in Python

last time in cs recitations. computer commands. today s topics.

CS 106A, Lecture 27 Final Exam Review 1

COMP String and Console I/O. Yi Hong May 18, 2015

Course Wrap-up. CSC207 Fall 2015

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

Simple Java Input/Output

Compiler Construction I

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Regular Expressions!!

CS 211 Regular Expressions

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Lesson 3: Accepting User Input and Using Different Methods for Output

Computer Systems and Architecture

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

We now start exploring some key elements of the Java programming language and ways of performing I/O

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

CS 261 Fall Mike Lam, Professor. Structs and I/O

CS61C : Machine Structures

Compiler Construction I

CS 520/620 Advanced Software Engineering Spring February 02, 2016

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

CSE : Python Programming

CIS192 Python Programming. Robert Rand. August 27, 2015

CS 106 Introduction to Computer Science I

CSE 401 Midterm Exam Sample Solution 11/4/11

Welcome to the Using Objects lab!

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Express Yourself. The Great Divide

Welcome to CSC148! Introduction to Computer Science

CSC324 Principles of Programming Languages

Getting started with Java

Index. caps method, 180 Character(s) base, 161 classes

ECE 122 Engineering Problem Solving with Java

CSC207 Week 2. Larry Zhang

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

Transcription:

CSC207 Week 9 Larry Zhang 1

Logistics A2 Part 2 is out. A1 Part 2 marks out on Peer Assessment, for remarking requests, contact Larry. 2

Today s outline File I/O Regular Expressions 3

File I/O: read and write files We use different kinds of I/O Stream classes to read and write files. Byte Streams: handle I/O of raw binary data. Character Streams: handle I/O of character data, automatically handling translation to and from the local character set. Buffered Streams: optimize input and output by reducing the number of calls to the native API. Scanner: allows a program to read and write formatted text. 4

Byte Stream Reads and writes one byte at a time. 5

Character Stream Reads and writes one char (two bytes) at a time. 6

Buffer Streams The previous streams are unbuffered streams, i.e., each read() and write() triggers a disk access at lower level. This can be very expensive (slow). Buffer streams: for read: one disk access reads a batch of data from the disk to a memory area (call buffer), then Java read() gets data from the buffer. for write: Java write() writes to the buffer (memory); when the buffer is full, flush the buffer to disk (batch write). Much smaller number of disk access, much more efficient. 7

Buffered Streams 8

Scanner We ve used scanner to handle user input from console (System.in), for files it s basically the same thing. In fact, the System.in and System.out are essentially files. 9

DEMO #1: FileIO.java 10

More I/O streams Data streams: read/write, int, float, double, etc https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html Object streams: read/write (serialized) objects https://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html 11

Regular Expressions Slides materials adopted from CSC207 Fall 2015 St George 12

What is a regular expression 13

Invented by Stephen Cole Kleene 14

Example Uses This regular expression (\d{3}-)?\d{3}-\d{4} mathes phone numbers like 905-555-1234 or 555-1234 This regular expression [a-za-z]\d[a-za-z]\s\d[a-za-z]\d matches postal codes like L5L 1c6 Very compact and efficient way to do string matching, but can be evil unless you fully master how to use it properly. 15

A Regex for Validating RFC822 Email Addresses 16

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski 17

Simple Patterns 18

Anchoring 19

Escaping 20

Predefined Character Classes 21

Define Your Own Character Classes 22

Quantifiers 23

Capturing Groups Capturing groups allow you to treat multiple characters as a single unit. Use parentheses to group. For example, (BC)* means zero or more instances of BC, e.g., BC, BCBC, BCBCBC, etc. 24

Capturing Groups are Numbered Capturing groups are numbered by counting their opening parentheses from left to right. For example, ((A)(B(C))) has the following groups: 1. 2. 3. 4. ((A)(B(C))) (A) (B(C)) (C) The numbers are useful because they can used as references to the groups. 25

Backreference The section of the input string matching the capturing group(s) is saved in memory for later recall via backreference. A backreference is specified in the regular expression as a backslash (\) followed by a digit indicating the number of the group to be recalled. For example, Pattern Example matching string (\d\d)\1 1212 (\w*)\s\1 asdf asdf 26

Exercise To match lines in a CSV file with format Name,StudentNumber,mark Examples: sid smith,999912345,23 jane samantha doe,1000123456,100 27

Attempts (.*),(.*),(.*) (.*),(\d*),(.*) (.*),(\d{9} \d{10}),(.*) (.*),(\d{9}\d?),(.*) (.*),(\d{9}\d?),((100) (\d\d)) (.*),(\d{9}\d?),((100) (\d?\d)) (.*),(\d{9}\d?),((100) ([1-9]?\d)) // good (.*),(\d{9,10}),((100) ([1-9]?\d)) // good 28

A given a set of strings that need to be matched, a proper regex for this set matches all strings in this set and does NOT match any string that is NOT in this set. 29

How to always come up with the proper regex? TRAIN YOURSELF TO SEE THE PATTERNS 30

Use Regex in Java A little DFA is built here by the compile method. 31

match() vs find() match() matches the entire string with the regex find() tries to find a substring that matches the regex For example 32

DEMO #2: RegexMatcher.java 33

There are different flavours of regex Different languages have different implementations of Regex which may behave differently. http://www.greenend.org.uk/rjk/tech/regexp.html Know your flavour before using it (read the manual). 34

References Good tutorials: https://regexone.com/ https://regex101.com/ 35