AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide.

Slides:



Advertisements
Similar presentations
Regular Expressions BKF03 Brian Ciccolo. Agenda Definition Uses – within Aspen and beyond Matching Replacing.
Advertisements

Tutorial 6 Creating a Web Form
Regular Expressions using Ruby Assignment: Midterm Class: CPSC5135U – Programming Languages Teacher: Dr. Woolbright Student: James Bowman.
REGULAR EXPRESSIONS FRIEND OR FOE?. INTRODUCTION TO 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.
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
Learning Ruby - 6 Regular Expressions. Ruby's Regex Technology Specifying what you are after using a terse, compact syntax Very useful when working with.
Scripting Languages Chapter 8 More About Regular Expressions.
Introduction to regular expression. Wéber André Objective of the training Scope of the course  We will present what are “regular expressions”
Filters using Regular Expressions grep: Searching a Pattern.
PRX Functions: There is Hardly Anything Regular About Them! Ken Borowiak.
slides created by Marty Stepp
Regex Wildcards on steroids. Regular Expressions You’ve likely used the wildcard in windows search or coding (*), regular expressions take this to the.
Regular Expressions. String Matching The problem of finding a string that “looks kind of like …” is common  e.g. finding useful delimiters in a file,
CMSC 330 Exercise: Write a Ruby function that takes an array of names in “Last, First Middle” format and returns the same list in “First Middle Last” format.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Last Updated March 2006 Slide 1 Regular Expressions.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
Regular Expressions in.NET Ashraya R. Mathur CS NET Security.
INFO 320 Server Technology I Week 7 Regular expressions 1INFO 320 week 7.
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
Regular Expressions – Friend or Foe? Primož Gabrijelčič.
CIS 451: Regular Expressions Dr. Ralph D. Westfall January, 2009.
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,
Regular Expressions CISC/QCSE 810. Recognizing Matching Strings ls *.exe translates to "any set of characters, followed by the exact string ".exe" The.
Regular Expressions CSC207 – Software Design. Motivation Handling white space –A program ought to be able to treat any number of white space characters.
Regular Expressions.
Regular Expression Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Post-Module JavaScript BTM 395: Internet Programming.
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.
Review Please hand in your practicals and homework Regular Expressions with grep.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
Satisfy Your Technical Curiosity Regular Expressions Roy Osherove Methodology & Team System Expert Sela Group The.
Regular Expressions What is this line all about? while (!($search =~ /^\s*$/)) { It’s a string search just like before, but with a huge twist – regular.
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,
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.
20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Perl Day 4. Fuzzy Matches We know about eq and ne, but they only match things exactly We know about eq and ne, but they only match things exactly –Sometimes.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
Regular Expressions Pattern and String Matching in Text.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
How Your Customers Will Pay Online & by Phone
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
Regular Expressions. What is it 4? Text searching & replacing Sequence searching (input, DNA) Sequence Tracking Machine Operation logic machines that.
Tutorial 6 Creating a Web Form
Copyright 2009 der.hans intro over a decade as sysadmin, programmer, analyst, data janitor currently director of engineering for a startup adjunct instructor.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
Looking for Patterns - Finding them with Regular Expressions
Ruby Regular Expressions
PHP / MySQL Introduction
Regular Expressions
An Overview of Grep and Regular Expression
1.5 Regular Expressions (REs)
More Variables and Data Types: references/matricies
REGEX.
Regular Expressions.
Presentation transcript:

AND OTHER LANGUAGES… Ruby Regular Expressions

Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide powerful search (via pattern matching) capabilities Simple regex are easy, but more advanced patterns can be created as needed From:

Regular Expressions in Ruby Objects of type Regexp Matched using =~ operator Constructed as  /pattern/  /pattern/options  %r{pattern}  %r{pattern}options  Regexp.new Options provide additional info about how pattern match should be done, for example:  i – ignore case  m – multiline, newline is an ordinary character to match  u,e,s,n – specifies encoding, such as UTF-8 (u) From:

Simple Example s = "ruby is cool" s2 = "i love ruby!" if s =~ /Ruby/i puts "found Ruby - case insensitive" end if s =~ /[Rr]uby/ puts "found Ruby - Rr option" end if s =~ /\Aruby/ puts "found Ruby at beginning" end if s2 =~ /\Aruby/ puts "found Ruby at beginning" else puts "ruby is not at the beginning" end s3 = "I love Java and Ruby and PHP" if s3 =~ /Java|PHP/ puts "what, another language?" end partNum = "aZ2" if partNum =~ /[a-z][A-Z][0-9]/ puts "part number is lower-case then upper-case then digit" end anotherPart = if anotherPart =~ puts "another part is any character, some end Play with Regex: rubular.com

Quick Exercise Create regex for the following. Use rubular.com to check it out. Phone numbers  (303)   Date  nn-nn-nn  Try some other options

Some Resources regular-expressions-in-ruby-part-1-of-3/ regular-expressions-in-ruby-part-1-of-3/ ial-guide-to-regular-expressions-tools-tutorials-and- resources/ ial-guide-to-regular-expressions-tools-tutorials-and- resources/ between-a-z-and-in-ruby-regular-expressions (thanks, Austin and Santi) between-a-z-and-in-ruby-regular-expressions

Topic Exploration not-use-regular-expressions not-use-regular-expressions advanced-regular-expressions/ advanced-regular-expressions/ from-strings from-strings A little more motivation to use… expressions expressions Submit on BB (3 points) and report back: 3-5 things you want to remember about regex. Include the URL. Feel free to read others not in the list.

In-class Challenge Review one of the regex cheat sheets Make up a pattern (e.g., part numbers, foreign telephone numbers, dates, parse an error log, etc.). Try to use as many aspects of regex as you can. For example, when I pay VISA online, my confirmation includes an embedded date and some other encoding. Create examples of your pattern Have your neighbor try to recreate your pattern. Nothing to submit. Is this exercise intended to explore good uses of regex? NO! It’s just a logic puzzle and refresher (or intro) to regex syntax.