Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to regular expression. Wéber André. - 2 - Objective of the training Scope of the course  We will present what are “regular expressions”

Similar presentations


Presentation on theme: "Introduction to regular expression. Wéber André. - 2 - Objective of the training Scope of the course  We will present what are “regular expressions”"— Presentation transcript:

1 Introduction to regular expression. Wéber André

2 - 2 - Objective of the training Scope of the course  We will present what are “regular expressions” as used in different unix tools, in some programming languages but also in XML Schema or in some text editors. Objectives  The trainee will know what are regular expressions.  The trainee will be motivated to go on this discovery of “regular expressions” by himself.

3 - 3 - Methods and tools The objectives of the training is to give you a first look at regular expressions.

4 - 4 - Agenda 1.Introduction 2.Syntax 3.Exercises

5 - 5 - Introduction A „regular expression“ is a string pattern. It will be used by a search engine to find (or replace) strings (or part of strings) that match the pattern. Such expression are used by different tools: grep / egrep / sed / find vi / emacs Ultraedit / crimson editor XML Schema (XSD)‏ Perl, javaScript, java, C#, PHP, Python, Tcl Microsoft taglibs...

6 Elements of the syntax Extract of : http://marvin.cs.uidaho.edu/~heckendo/Handouts/regex.html

7 Elements of the syntax …

8 - 8 - Patterns – Elements of the syntax Quantifiers : *, +, ?, {n}, {n,}, {n,m} Group : ( )‏ or \( \) – only for grep. Anchors : ^, $ Escape : \ Reference to the substring matching a group : \ Option : | Boundaries : \b Special chars : \t, \n Character classes :., \d, \w, \s, \D, \W, \S One char amongst a list : [abc] or [a-c] One char not in a list : [^abc] or [^a-c] Any char, except “\n” :. Search the shorter match : “?“ after the quantifier, example :.*? Optional pattern : “?” after the pattern Legend: In red, elements not recognized by grep and egrep. In blue, elements not recognized by grep but by egrep. In green, elements in grep but not in egrep. All elements (expect grouping for grep) are recognized par Perl.

9 - 9 - The syntax explained with examples. (hello|bonjour) : match „hello“ or „bonjour“ \b(hello\s+)+ : one or more occurrences of the word „hello“ [a-z][a-zA-Z0-9_]* : pattern for a convention for variable names in Java [^<&]* : a simplified pattern for PC_DATA in XML ([\w\d]*,)*([\w\d]*)\n : a simplified pattern for a record of a CSV file ^([^#][^ ]*)= *(.*)$ : a line of a property file with field=value [^\\]*$ : the name of the file in a file path : a CDATA section in XML "([^"\n]|\\")*" : a literal string in a java source file

10 - 10 - Exercises Find the regular expression for the path to the directory in a file path the validation of an eMail address. Example : “awe@c-w.be” the validation of a phone number. Example “+352 46.65.48.252” retrieving the names of the programs to generate with a Cobol “model” in Thaler. Example : “* Programs generated by this model : ACA2, ACC2, ACD2, ACM2 *”. the validation of an URL and the isolation of its different elements : protocol, address, port, file or program, path, arguments. Example : http://www.noWhere.com:80/prog.php/docs/2008?id=100&lang=FR http://www.noWhere.com:80/prog.php/docs/2008?id=100&lang=FR the extraction of an xml element (not containing itself).

11 That’s all folks!


Download ppt "Introduction to regular expression. Wéber André. - 2 - Objective of the training Scope of the course  We will present what are “regular expressions”"

Similar presentations


Ads by Google