Regular Expressions Dr. Ralph D. Westfall May, 2011.

Slides:



Advertisements
Similar presentations
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Advertisements

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.
Computer & Network Forensics
Using regular expressions Search for a single occurrence of a specific string. Search for all occurrences of a string. Approximate string matching.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Tutorial 14 Working with Forms and Regular Expressions.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
Scripting Languages Chapter 8 More About Regular Expressions.
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,
More on Regular Expressions Regular Expressions More character classes \s matches any whitespace character (space, tab, newline etc) \w matches.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Last Updated March 2006 Slide 1 Regular Expressions.
Introduction to Internet Engineering Tutorial 7 All about Assignment 2 By Tse Hok
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
Tutorial 14 Working with Forms and Regular Expressions.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
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’
Regular Expressions in.NET Ashraya R. Mathur CS NET Security.
Faculty of Sciences and Social Sciences HOPE JavaScript Validation Regular Expression Stewart Blakeway FML
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital Media.
Introduction to Computing Using Python Regular expressions Suppose we need to find all addresses in a web page How do we recognize addresses?
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
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,
CIS 451: XML DTDs Dr. Ralph D. Westfall February, 2009.
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
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.
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.
VBScript Session 13.
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.
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 for PHP Adding magic to your programming. Geoffrey Dunn
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,
20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
JavaScript III ECT 270 Robin Burke. Outline Validation examples password more complex Form validation Regular expressions.
1 Validating user input is the bane of every software developer’s existence. When you are developing cross-browser web applications (IE4+ and NS4+) this.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Variable Variables A variable variable has as its value the name of another variable without $ prefix E.g., if we have $addr, might have a statement $tmp.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
1 Week 8 Final Project Planning & Chapter 6 JavaScript Advanced Web Development IT225 Spring Term 2016 Marymount University School of Business Administration.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Computer Science I Split. Regular Expressions Classwork: Trivia questions. Share. Show (stage 1) final project. Homework: work on final project.
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.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Regular Expressions.
Regular Expressions Upsorn Praphamontripong CS 1110
Regular Expressions 'RegEx'.
CS 330 Class 7 Comments on Exam Programming plan for today:
Looking for Patterns - Finding them with Regular Expressions
Pattern Matching in Strings
Working with Forms and Regular Expressions
Matcher functions boolean find() Attempts to find the next subsequence of the input sequence that matches the pattern. boolean lookingAt() Attempts to.
Regular Expressions and Grep
Lecture 25: Regular Expressions
REGEX.
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

Regular Expressions Dr. Ralph D. Westfall May, 2011

RegEx Object what does it do? finds patterns in text  specific strings (all characters identified)  strings with "wild cards" (any character)  strings with certain characters, not others how could it be used in VB.NET? validating inputs on an input form e.g.,  addresses, telephone #s, etc. searching the content of a large database

Regular Expressions based on the idea of wild cards.e.g., dir s*.doc in DOS/Windows finds any.doc file starting with s, followed by any other character(s) find much more complicated patterns e.g., any of a specified list of characters rather than just one or all possibilities e.g., any character except ones in a list

Regular Expression Object put import statement at top of code, just below Option Strict on Imports System.Text.RegularExpressions create a regular expression object Dim reg as Regex reg = New Regex("[pattern]") need to create a Regex with a pattern (can't add one later)

VB.NET RegEx Special Characters. ba. finds bat, bal, bac period matches any single character ? bottles? finds bottle and bottles 0 or 1 of what is before the ? [0-9] finds 0, 1, … or 9 any 1 character in specified range + to+ finds to and too 1 or more of what is before + {#} b{2} finds any bb # = number of preceding matches

RegEx Special Characters - 2 | OR operator e.g., a|b finds a or b \ escape character is used to revert special characters to literal values e.g., \. (back slash followed by period becomes an actual period change regular characters to operators  \b finds a word boundary (1 st or last character) or replaces a backspace

Combining Operators can group character sequences [a-z]{3} finds 3-letter lower case words [0-9]{4}( |-)? finds any 4 #s followed by space or dash or neither (could use in credit card validation)

Regular Expression Methods reg.IsMatch([string to search]) returns true if pattern is found in string reg.Match([string to search]) returns a match object each time pattern is found (use ToString to get object value) reg.Replace([string1, string2]) replaces every occurrence of string1 with string2‘Notes

Regular Expression Match Object can tell if a match was found can also return the matching string usually don't need the whole string, just want the matching part mach = reg.Match([string to search]) 'returns match object wherever pattern is found If mach.Success Then strZip = mach.ToString

Using Regular Expressions Dim strCard as String = TextBox1.Text Dim mach as Match Dim reg As New Regex("[0-9]{4}( |-)?" & _ "[0-9]{4}( |-)?[0-9]{4}( |-)?[0-9]{4}")'shorten? mach = reg.Match(strCard ) If mach.Success Then MsgBox("Card is OK") strCard = mach.ToString.Replace("-"c, " "c) TextBox1.Text = strCard ' finds pattern even inside other characters ' e.g., XYZ ABC '.Replace("-", " ") dashes  spaces)

Warning a regular expression may say a String is OK even if there are other characters around it e.g., so need to extract match from original String (other processing?) Dim strCard as String Dim ok as Boolean strCard = "XYZ XYZ" ok = reg.IsMatch(strCard) 'credit card If ok Then 'pattern MsgBox(strCard & " is OK") End If

Extracting Matches from Strings can use.Match() function to separate matching part from other characters around it e.g., Dim strCard as String strCard = "XYZ XYZ" If reg.IsMatch(strCard) Then MsgBox(reg.Match(strCard).ToString & "is OK") End If

More Regular Expressions Info Regular Expressions in.Net JavaScript Regular Expressions Tester A Better.NET Regular Expression Tester Tee-Shirts, etc.Tee-Shirts, etc. (language alert)

RegEx Exercises 1 explain the following address pattern: 2 extend it to handle following endings: com, edu, org, net, gov, mil, int (must appear at least once) 3 modify it to allow numbers, dashes [-] and periods [.] after the 1st character

RegEx Exercises - 2 create patterns to validate zip codes both as 5-digit and Zip + 4  e.g., or phone #s (intnl., long distance, and local) Social Security #s (or spaces) Cal Poly student ID numbers names (including middle initial, von, de, etc.) course #s (CIS, EBZ, CS; 1xx-4xx, etc.)

RegEx Exercises - 3 test text or files samples against regular expressions at Regular Expression Library's tester page and report back, using regular expressionstester from previous pages in this PowerPoint others that you make up search for examples from Regular Expression LibraryRegular Expression Library

RegEx Exercises - 4 use Regular Expression Library's tester page to Load a Data Source from a URL and find data in the web page using a regular expression that you specifytester and/or find free (trial) software that will do the same thing