PHP –Regular Expressions

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Lexical Analysis Consider the program: #include main() { double value = 0.95; printf("value = %f\n", value); } How is this translated into meaningful machine.
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 Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/28.
Using regular expressions Search for a single occurrence of a specific string. Search for all occurrences of a string. Approximate string matching.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
Basic Elements of C++ Chapter 2.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
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,
Lecture 10A Perl: Programming Freedom Fundamentals of Engineering For Honors – H192 By Robert Mohr, Ted Pavlic, and Joe Ryan.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
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,
Strings, output, quotes and comments
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.
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional.
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Lex & Yacc By Hathal Alwageed & Ahmad Almadhor. References *Tom Niemann. “A Compact Guide to Lex & Yacc ”. Portland, Oregon. 18 April 2010 *Levine, John.
CompSci 101 Introduction to Computer Science November 18, 2014 Prof. Rodger.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Models of Computing Regular Expressions 1. Formal models of computation What can be computed? What is a valid program? What is a valid name of a variable.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Regular Expressions. What is it 4? Text searching & replacing Sequence searching (input, DNA) Sequence Tracking Machine Operation logic machines that.
Regular Expressions In Javascript cosc What Do They Do? Does pattern matching on text We use the term “string” to indicate the text that the regular.
Chapter Topics The Basics of a C++ Program Data Types
Regular Expressions Upsorn Praphamontripong CS 1110
Perl Regular Expression in SAS
Looking for Patterns - Finding them with Regular Expressions
Chapter 3 Lexical Analysis.
System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007 References: Perl man pages Albert Lingelbach, Jr.
Tutorial On Lex & Yacc.
Basic Elements of C++.
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Intro to PHP & Variables
Working with Forms and Regular Expressions
Basic Elements of C++ Chapter 2.
CSC 352– Unix Programming, Spring 2016
PHP.
CSCI 431 Programming Languages Fall 2003
Selenium WebDriver Web Test Tool Training
Data Manipulation & Regex
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
More Variables and Data Types: references/matricies
Lab 8: Regular Expressions
PHP and JSON Topics Review JSON.
REGEX.
Perl Regular Expressions – Part 1
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

PHP –Regular Expressions Reading contents of a file PHP Language constructs Regular Expressions

Reading the contents of a file Option 1: file_get_contents() Using this function will return the contents of a given file as a single string. Option 2: file() Reads an entire file into an array. Each cell contains a string of text (paragraph).

Language Constructs "Language constructs" are operations that are supported by special features in the language. In PHP, mostly anything that isn't a variable or a function is a language construct. These can be found at the PHP documentation page: http://php.net/manual/en/reserved.keywords.php   TIP: list() function is used to assign a list of variables in one operation. <?php $my_array = array("Dog","Cat","Horse"); list($a, $b, $c) = $my_array; echo "I have several animals, a $a, a $b and a $c."; ?> Output: I have several animals, a Dog, a Cat and a Horse.

Regular Expressions and preg_split() A rational expression is referred to as a regular expression. A regular expression is a sequence of characters that can be defined by a pattern. Pattern matching with strings is important in many areas of computer science,but especially with the web. Data is often passed as a string –it must be parsed and dissembled into meaningful objects.

PHP and Regular Expressions PHP relies on a method called preg_split() for processing regular expressions. preg_split() is used to split a string into an array of variables based on a pattern of characters. preg means Pcre REGexp". "PCRE" means "Perl Compatible Regexp", which is functionality for processing a “regular expression”.   The main rule for processing a regular expression is the consistent escaping rule. PCRE has consistent escaping rules: any non-alpha-numeric character may be escaped to mean its literal value by prefixing a \ (backslash) before the character, and vice versa.

Example: Pattern Each line contains a food object. For each line, tabs separate three elements: Word food type definition

Code Tasks: a. Open the file and copy the contents to an array variable. b. Shuffle the array. c. Select a random line from the array. $lines = file(“dataFile.txt”); Shuffle($lines); $aLine = trim($lines[array_rand($lines)]);

Code Task: For a given line, create three variables, one for each element. list($food, $foodType, $description) = preg_split(“/[t]+/”, $aLine); Explanation : "/[\t]+/“ / = start or end of pattern string [ ... ] = grouping of characters + = one or more of the preceding character or group \t = A tab character.

More Explanations -------------------------------------------------------------------------------- \w word characters (a-z, A-Z, 0-9, _) ) end of look-behind \b the boundary between a word char (\w) and something that is not a word char \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) [!?.]* any character of: '!', '?', '.' (0 or more times (matching the most amount possible))