Regular Expressions using Ruby Assignment: Midterm Class: CPSC5135U – Programming Languages Teacher: Dr. Woolbright Student: James Bowman.

Slides:



Advertisements
Similar presentations
Regular Expressions in Perl By Josue Vazquez. What are Regular Expressions? A template that either matches or doesn’t match a given string. Often called.
Advertisements

Regular Expressions (in Python). Python or Egrep We will use Python. In some scripting languages you can call the command “grep” or “egrep” egrep pattern.
Python: Regular Expressions
AND FINITE AUTOMATA… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 2: 8/23.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
W3101: Programming Languages (Perl) 1 Perl Regular Expressions Syntax for purpose of slides –Regular expression = /pattern/ –Broader syntax: if (/pattern/)
CS 330 Programming Languages 10 / 10 / 2006 Instructor: Michael Eckmann.
More Regular Expressions. List/Scalar Context for m// Last week, we said that m// returns ‘true’ or ‘false’ in scalar context. (really, 1 or 0). In list.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
Scripting Languages Chapter 8 More About Regular Expressions.
HTML Forms Validation CS360 Javascript. On to forms processing... The processing of a form is done in two parts: –Client-side at the browser, before the.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Regular Expressions Dr. Ralph D. Westfall May, 2011.
Language Recognizer Connecting Type 3 languages and Finite State Automata Copyright © – Curt Hill.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
1 Flex. 2 Flex A Lexical Analyzer Generator  generates a scanner procedure directly, with regular expressions and user-written procedures Steps to using.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Regular Expressions in Perl Part I Alan Gold. Basic syntax =~ is the matching operator !~ is the negated matching operator // are the default delimiters.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Python Regular Expressions Easy text processing. Regular Expression  A way of identifying certain String patterns  Formally, a RE is:  a letter or.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
BY Sandeep Kumar Gampa.. What is Regular Expression? Regex in.NET Regex Language Elements Examples Regular Expression API How to Test regex in.NET Conclusion.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
VBScript Session 13.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
C# Strings 1 C# Regular Expressions CNS 3260 C#.NET Software Development.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Regular Expressions. Overview Regular expressions allow you to do complex searches within text documents. Examples: Search 8-K filings for restatements.
Module 6 – Generics Module 7 – Regular Expressions.
Regular Expressions in Perl CS/BIO 271 – Introduction to Bioinformatics.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
CS346 Regular Expressions1 Pattern Matching Regular Expression.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
R EGULAR E XPRESSION IN P ERL (P ART 1) Thach Nguyen.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
1 Validating user input is the bane of every software developer’s existence. When you are developing cross-browser web applications (IE4+ and NS4+) this.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 5 Regular Expressions.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image. ADVANCED.
What is grep ?  % man grep  DESCRIPTION  The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
CMSC330 More Ruby. Last lecture Scripting languages Ruby language –Implicit variable declarations –Many control statements –Classes & objects –Strings.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
RE Tutorial.
Regular Expressions Upsorn Praphamontripong CS 1110
Ruby Regular Expressions
Pattern Matching in Strings
CS 1111 Introduction to Programming Fall 2018
CIT 383: Administrative Scripting
REGEX.
Presentation transcript:

Regular Expressions using Ruby Assignment: Midterm Class: CPSC5135U – Programming Languages Teacher: Dr. Woolbright Student: James Bowman

Regular Expression Definition  Regular Expressions provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.  Examples: Social Security Number(XXX-XX-XXXX), Telephone number(XXX-XXX-XXXX), or phrases to check for(“Cat Name: XXXX”).

Regular Expression Modifiers ModifierDescription iIgnore case when matching text. oPerform #{} interpolations only once, the first time the regexp literal is evaluated. xIgnores whitespace and allows comments in regular expressions mMatches multiple lines, recognizing newlines as normal characters u,e,s,nInterpret the regexp as Unicode (UTF- 8), EUC, SJIS, or ASCII. If none of these modifiers is specified, the regular expression is assumed to use the source encoding.

Regular Expression Patterns Pattern:Description: ^Matches beginning of line. $Matches end of line..Matches any single character except newline. Using m option allows it to match newline as well. [...]Matches any single character in brackets. [^...]Matches any single character not in brackets re*Matches 0 or more occurrences of preceding expression. re+Matches 1 or more occurrence of preceding expression. re?Matches 0 or 1 occurrence of preceding expression. re{ n}Matches exactly n number of occurrences of preceding expression. re{ n,}Matches n or more occurrences of preceding expression. re{ n, m}Matches at least n and at most m occurrences of preceding expression.

Regular Expression Patterns Cont… Pattern: Description: a| bMatches either a or b. (re)Groups regular expressions and remembers matched text. (?imx)Temporarily toggles on i, m, or x options within a regular expression. If in parentheses, only that area is affected. (?-imx)Temporarily toggles off i, m, or x options within a regular expression. If in parentheses, only that area is affected. (?: re)Groups regular expressions without remembering matched text. (?imx: re)Temporarily toggles on i, m, or x options within parentheses. (?-imx: re)Temporarily toggles off i, m, or x options within parentheses. (?#...)Comment. (?= re)Specifies position using a pattern. Doesn't have a range. (?! re)Specifies position using pattern negation. Doesn't have a range. (?> re)Matches independent pattern without backtracking.

Regular Expression Patterns Cont… Pattern: Description: \wMatches word characters. \WMatches nonword characters. \sMatches whitespace. Equivalent to [\t\n\r\f]. \SMatches nonwhitespace. \dMatches digits. Equivalent to [0-9]. \DMatches nondigits. \AMatches beginning of string. \ZMatches end of string. If a newline exists, it matches just before newline. \zMatches end of string. \GMatches point where last match finished. \bMatches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. \BMatches nonword boundaries. \n, \t, etc.Matches newlines, carriage returns, tabs, etc. \1...\9Matches nth grouped subexpression. \10Matches nth grouped subexpression if it matched already. Otherwise refers to the octal representation of a character code.

Regular Expression Examples Example:Description: /[Rr]uby/Match "Ruby" or "ruby" /rub[ye]/Match "ruby" or "rube" /[aeiou]/Match any one lowercase vowel /[0-9]/Match any digit; same as /[ ]/ /[a-z]/Match any lowercase ASCII letter /[A-Z]/Match any uppercase ASCII letter /[a-zA-Z0-9]/Match any of the above /[^aeiou]/Match anything other than a lowercase vowel /[^0-9]/Match anything other than a digit Character classes:

Regular Expression Examples Example:Description: /./Match any character except newline /./mIn multiline mode. matches newline, too /\d/Match a digit: /[0-9]/ /\D/Match a nondigit: /[^0-9]/ /\s/Match a whitespace character: /[ \t\r\n\f]/ /\S/Match nonwhitespace: /[^ \t\r\n\f]/ /\w/Match a single word character: /[A-Za- z0-9_]/ /\W/Match a nonword character: /[^A-Za- z0-9_]/ Special Character Classes:

Regular Expression Examples Example:Description: /ruby|rube/Match "ruby" or "rube" /rub(y|le))/Match "ruby" or "ruble" /ruby(!+|\?)/"ruby" followed by one or more ! or one ? Alternatives:

Regular Expression Examples ExampleDescription /^Ruby/Match "Ruby" at the start of a string or internal line /Ruby$/Match "Ruby" at the end of a string or line /\ARuby/Match "Ruby" at the start of a string /Ruby\Z/Match "Ruby" at the end of a string /\bRuby\b/Match "Ruby" at a word boundary /\brub\B/\B is nonword boundary: match "rub" in "rube" and "ruby" but not alone /Ruby(?=!)/Match "Ruby", if followed by an exclamation point /Ruby(?!!)/Match "Ruby", if not followed by an exclamation point Anchors:

Regexp Class  A Regexp holds a regular expression, used to match a pattern against strings. Regexps are created using the /…/ and %r{…} literals, and by the Regexp::new constructor.

Regexp Class  Escape:  Escapes any characters that would have special meaning in a regular expression. Example: Regexp.escape('\*?{}.') #=> \\\*\?\{\}\.

Regexp Class  New:  Constructs a new regular expression from pattern, which can be a string. Example: r1 = Regexp.new('^a- z+:\\s+\w+') #=> /^a-z+:\s+\w+/

Search and Replace Example #!/usr/bin/ruby phone = " #This is Phone Number" # Delete Ruby-style comments phone = phone.sub!(/#.*$/, "") puts "Phone Num : #{phone}" # Remove anything other than digits phone = phone.gsub!(/\D/, "") puts "Phone Num : #{phone}" Result: Phone Num : Phone Num :

Code Example – Matching on Strings #!/usr/bin/ruby line1 = “People are smarter than dogs"; line2 = "Dogs also like cats"; if ( line1 =~ /People(.*)/ ) puts "Line1 starts with People" end if ( line2 =~ /People(.*)/ ) puts "Line2 starts with Dogs" end Result: Line1 starts with People

Code Examples Please See Attached Code for more examples