Appendix A: Regular Expressions It’s All Greek to Me.

Slides:



Advertisements
Similar presentations
CSCI 330 T HE UNIX S YSTEM Regular Expressions. R EGULAR E XPRESSION A pattern of special characters used to match strings in a search Typically made.
Advertisements

Regular Expressions grep
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller
Quotes: single vs. double vs. grave accent % set day = date % echo day day % echo $day date % echo '$day' $day % echo "$day" date % echo `$day` Mon Jul.
Regular Expressions. u A regular expression is a pattern which matches some regular (predictable) text. u Regular expressions are used in many Unix utilities.
Using regular expressions Search for a single occurrence of a specific string. Search for all occurrences of a string. Approximate string matching.
Regular Expressions In ColdFusion and Studio. Definitions String - Any collection of 0 or more characters. Example: “This is a String” SubString - A segment.
CSE467/567 Computational Linguistics Carl Alphonce Computer Science & Engineering University at Buffalo.
Filters using Regular Expressions grep: Searching a Pattern.
Regular Expression A regular expression is a template that either matches or doesn’t match a given string.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Regular Language & Expressions. Regular Language A regular language is one that a finite state machine (fsm) will accept. ‘Alphabet’: {a, b} ‘Rules’:
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
Last Updated March 2006 Slide 1 Regular Expressions.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
System Programming Regular Expressions Regular Expressions
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
INFO 320 Server Technology I Week 7 Regular expressions 1INFO 320 week 7.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
Regular expressions Used by several different UNIX commands, including ed, sed, awk, grep A period ‘.’ matches any single characters.X. matches any X.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/15.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.
Regular Expression in Java 101 COMP204 Source: Sun tutorial, …
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
Quiz 30 minutes 10 questions No talking, texting, collaboration, etc…
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
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.
Pattern Matching CSCI N321 – System and Network Administration.
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.
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
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Unix Programming Environment Part 3-4 Regular Expression and Pattern Matching Prepared by Xu Zhenya( Draft – Xu Zhenya(
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 DIG 3563: Lecture 2a: Regular Expressions Michael Moshell University of Central Florida Information Management.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
JavaScript III ECT 270 Robin Burke. Outline Validation examples password more complex Form validation Regular expressions.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 5 – Regular Expressions, grep, Other Utilities.
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.
Adv. UNIX: REs/31 Advanced UNIX v Objectives –explain how to write Regular Expressions (REs) in vi and grep Special Topics in Comp. Eng.
-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.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
Hands-on Regular Expressions Simple rules for powerful changes.
Regular Expressions Copyright Doug Maxwell (
Looking for Patterns - Finding them with Regular Expressions
Regular Expression - Intro
Lecture 9 Shell Programming – Command substitution
Folks Carelli, Instructor Kutztown University
Unix Talk #2 (sed).
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CSE 303 Concepts and Tools for Software Development
CSCI The UNIX System Regular Expressions
1.5 Regular Expressions (REs)
Regular Expressions grep Familiy of Commands
Presentation transcript:

Appendix A: Regular Expressions It’s All Greek to Me

Regular Expressions A pattern that matches a set of one or more strings May be a simple string, or contain wildcard characters or modifiers Used by programs such as vim, grep, awk, and sed Not the same as shell expansion

Components Characters –Literals –Special Characters Delimiters –Mark beginning end of regular expressions –Usually / –’ (but not really)

Simple Strings Contain no special characters Matches only the string Ex: /foo/ matches: –foo –tomfoolery –bar.foo.com

Special Characters Can match multiple strings Represent zero or more characters Always match the longest possible string (we’ll see examples in a bit)

Periods Matches any single character Ex: /.ing/ –I was talking –bling –he called ingred Ex: /spar.ing/ –sparring –sparking

Brackets Define a character class Match any one character in the class If a carat ( ^ ) is first character in class, character class matches any character not in class Other special characters in class lose meaning

Brackets con’t Ex. /[jJ]ustin/ matches justin and Justin Ex. /[A-Za-z]/ matches any letter Ex. /[0-9]/ matches any number Ex. /[^a-z]/ matches anything but lowercase letters

Asterisks Zero or more occurrences of the previous character So match any number of characters would be /.*/ Ex. /t.*ing/ –thing –this is really annoying

Plus Signs and Question Marks Very similar to asterisks, depend on previous + matches one or more occurrences (not 0) ? Matches zero or one occurrence (no more) Ex. /2+4?/ matches one or more 2’s followed by either zero or one 4 –22224, 2 match –4, 244 do not Part of the class of extended R.E.

Carets & Dollar Signs If a regular expression starts with a ^, the string must be at the beginning of a line If a regular expression ends with a $, the string must be at the end of a line ^ and $ are referred to as anchors Ex. /^T.*T$/ matches any line that starts and ends with T

Quoting Special Characters If you want to use a special character literally, put a backslash in front of it Ex. /and\/or/ matches and/or Ex. /\\/ matches \ Ex. /\**/ matches any number of asterisks

Longest Match Regular expressions match the longest string possible in a line Ex. I (Justin) like coffee (lots). /(.*)/ –Matches (Justin) like coffee (lots) /([^)]*)/ –Matches (Justin)

Boolean OR You can pattern match for two distinct strings using OR (the pipe) Ex. /CAT|DOG/ –Matches exactly CAT and exactly DOG Simplier expressions can be written just using a character class –I.E. /a[bc]/ instead of /ab|ac/ Also part of extended R.E.

Grouping You can apply special characters to groups of characters in parenthesis Also called bracketing Matches same as unbracketed expression But can use modifiers Ex. /\(duck\)*|\(goose\)/

Using with vim Use regular expressions for searching and substituting Searching: –/string or ?string Substituting: –:[g][address]s/string/replace[/g] –g : global; substitute all lines –string and replace can be R.E. –/g : global; replace all occurrences in the line

Using with vim con’t [address] –n : line number –n[+/-]x : line number plus x lines before or after –n1,n2 : from line n1 to n2 –. : alias for current line –$ : alias for last line in work buffer –% : alias for entire work buffer

vim examples /^if( /end\.$ :%s/[Jj]ustin/Mr\. Awesome/g

Using with vim con’t Ampersand (&) –Alias for matched string when substituting –Ex: /[A-Z][0-9]/_&_/ Quoted digit (\n) –Used with R.E. with multiple quoted parts –Can be used to rearrange columns –Ex: /\([^,]*\), \(.*\)/\2 \1/

Using with grep To take advantage of extended regular expressions, use egrep or grep -E instead Use single quote as delimiter Ex: –egrep ’^T.*T$’ myfile Lists all lines in myfile that begin & end with T