- Regular expressions:

Slides:



Advertisements
Similar presentations
Perl & Regular Expressions (RegEx)
Advertisements

Searching using regular expressions. A regular expression is also a ‘special text string’ for describing a search pattern. Regular expressions define.
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
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.
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
COS 381 Day 19. Agenda  Assignment 5 Posted Due April 7  Exam 3 which was originally scheduled for Apr 4 is going to on April 13 XML & Perl (Chap 8-10)
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
Cos 381 Day 4. © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Exam 1 Corrected –3 A’s, 3 B’s and 2 C’s I have looked at all the assignment.
CS 330 Programming Languages 10 / 10 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Cos 381 Day 4. © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Questions? Assignment 1 due January 31 (TUESDAY) Discussions on JAVASCRIPT.
Scripting Languages Chapter 8 More About Regular Expressions.
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.
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’
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 Expression JavaScript Web Technology Derived from:
Chapter 4 The Basics of JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Overview of JavaScript - Originally developed by Netscape,
Chapter 4 © 2010 by Addison Wesley Longman, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expressions – An Overview Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in.
DHTML1-1 JavaScript and HTML Documents Xingquan (Hill) Zhu
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Regular Expressions in Perl CS/BIO 271 – Introduction to Bioinformatics.
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.
©Brooks/Cole, 2001 Chapter 9 Regular Expressions.
CS346 Regular Expressions1 Pattern Matching Regular Expression.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
Pattern Matching II. Greedy Matching When dealing with quantifiers, Perl’s pattern matcher is by default greedy. For example, –$_ = “Bob sat next to the.
R EGULAR E XPRESSION IN P ERL (P ART 1) Thach Nguyen.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
Chapter 4 © 2014 by Pearson Education Overview of JavaScript - Originally developed by Netscape by Brendan Eich, as LiveScript - Became a joint venture.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 5 Regular Expressions.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
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.
Chapter 4 © 2013 by Pearson Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape and Sun.
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
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.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
RE Tutorial.
4.1 Overview of JavaScript
CSC 594 Topics in AI – Natural Language Processing
Regular Expressions and perl
CSC 594 Topics in AI – Natural Language Processing
The Basics of JavaScript
CSCI 431 Programming Languages Fall 2003
Selenium WebDriver Web Test Tool Training
CIT 383: Administrative Scripting
4.1 Overview of JavaScript
Regular Expressions in Java
Regular Expression in Java 101
Regular Expression: Pattern Matching
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Perl Regular Expressions – Part 1
Chapter 2 Reference Types.
Regular Expressions.
Presentation transcript:

- Regular expressions: Pattern Matching - Regular expressions: - Simple patterns - Two categories of characters in patterns: a. normal characters (match themselves) b. metacharacters (can have special meanings in patterns--do not match themselves) \ | ( ) [ ] { } ^ $ * + ? . - A metacharacter is treated as a normal character if it is backslashed - period is a special metacharacter - it matches any character except newline © 2006 Pearson Addison-Wesley. All rights reserved.

Pattern Matching (continued) search(pattern) - Returns the position in the object string of the pattern (position is relative to zero); returns -1 if it fails $str = "Gluckenheimer"; $position = $str.search(/n/); /* position is now 6 */ - Character classes - Put a sequence of characters in brackets, and it defines a set of characters, any one of which matches [abcd] - Dashes can be used to specify spans of characters in a class [a-z] - A caret at the left end of a class definition means the opposite [^0-9] © 2006 Pearson Addison-Wesley. All rights reserved.

Pattern Matching (continued) - Character classes (continued) - Character class abbreviations Abbr. Equiv. Pattern Matches \d [0-9] a digit \D [^0-9] not a digit \w [A-Za-z_0-9] a word character \W [^A-Za-z_0-9] not a word character \s [ \r\t\n\f] a whitespace character \S [^ \r\t\n\f] not a whitespace character - Variables in patterns are interpolated - Quantifiers - Quantifiers in braces Quantifier Meaning {n} exactly n repetitions {m,} at least m repetitions {m, n} at least m but not more than n repetitions © 2006 Pearson Addison-Wesley. All rights reserved.

Pattern Matching (continued) - Quantifiers (continued) - Other quantifiers (just abbreviations for the most commonly used quantifiers) - * means zero or more repetitions e.g., \d* means zero or more digits - + means one or more repetitions e.g., \d+ means one or more digits - ? Means zero or one e.g., \d? means zero or one digit © 2006 Pearson Addison-Wesley. All rights reserved.

Pattern Matching (continued) - Anchors - The pattern can be forced to match only at the left end with ^; at the end with $ e.g., /^Lee/ matches "Lee Ann" but not "Mary Lee Ann" /Lee Ann$/ matches "Mary Lee Ann", but not "Mary Lee Ann is nice" - The anchor operators (^ and $) do not match characters in the string--they match positions, at the beginning or end - Pattern modifiers - The i modifier tells the matcher to ignore the case of letters /oak/i matches "OAK" and "Oak" and … © 2006 Pearson Addison-Wesley. All rights reserved.

Pattern Matching (continued) - Pattern modifiers (continued) - The x modifier tells the matcher to ignore whitespace in the pattern (allows comments in patterns) - Other Pattern Matching Methods of String replace(pattern, string) - Finds a substring that matches the pattern and replaces it with the string (g modifier can be used) $str = "Some rabbits are rabid"; $str.replace(/rab/g, "tim"); str is now "Some timbits are timid" $1 and $2 are both set to "rab" © 2006 Pearson Addison-Wesley. All rights reserved.

Pattern Matching (continued) match(pattern) - The most general pattern-matching method - Returns an array of results of the pattern- matching operation - With the g modifier, it returns an array of the substrings that matched - Without the g modifier, first element of the returned array has the matched substring, the other elements have the values of $1, … $str = "My 3 kings beat your 2 aces"; $matches = $str.match(/[ab]/g); - matches is set to ["b", "a", "a"] split(parameter) "," and /,/ both work © 2006 Pearson Addison-Wesley. All rights reserved.