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.

Slides:



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

Python: Regular Expressions
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions.
Scripting Languages Chapter 8 More About Regular Expressions.
Form Validation CS What is form validation?  validation: ensuring that form's values are correct  some types of validation:  preventing blank.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
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.
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’
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
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?
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Finding the needle(s) in the textual haystack
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
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,
Strings in PHP Working with Text in PHP Strings and String Functions Mario Peshev Technical Trainer Software University
Regular Expressions.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 2 Variables.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Characters and Strings
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
More String Manipulation. Programming Challenge Define a function that accepts two arguments: a string literal and a single character. Have the function.
ICS611 Lex Set 3. Lex and Yacc Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the.
Awk 2 – more awk. AWK INVOCATION AND OPERATION the "-F" option allows changing Awk's "field separator" character. Awk regards each line of input data.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
28 Formatted Output.
Finding the needle(s) in the textual haystack
Chapter 2 Variables.
Regular Expressions 'RegEx'.
Looking for Patterns - Finding them with Regular Expressions
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Lecture 19 Strings and Regular Expressions
Strings, Characters and Regular Expressions
© 2016 Pearson Education, Ltd. All rights reserved.
Finding the needle(s) in the textual haystack
Finding the needle(s) in the textual haystack
Strings Part 1 Taken from notes by Dr. Neil Moore
INPUT & OUTPUT scanf & printf.
Data Manipulation & Regex
Chapter 2: Introduction to C++.
Introduction to Java Applications
Regular Expressions and Grep
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Lecture 25: Regular Expressions
Chapter 2 Variables.
REGEX.
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
What We Want To Do User enters: Mary Smith
Presentation transcript:

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 to validate user data or extract data from text files, you need to know how to handle strings. Basic String Function substr() The substr() function returns a part of a string. It receives 3 arguments : substr( string source, int begin, int length) the first character is counted as zero The third argument is optional

String Manipulation Lesson 4

String Manipulation Lesson 4 trim() Useful for cleaning up user input, trim() simply strips the white space characters (spaces, tabs, new lines) from the beginning and the end of a strings and returns the trimmed strings If you wish to only trim the beginning of the string, use ltrim(). To trim the end of a string, use chop()

String Manipulation Lesson 4 strlen() Returns the number of characters in a string

String Manipulation Lesson 4 chr() chr() receives an integer that represents an ASCII code, and returns the corresponding character. echo (chr(34)); // Prints a quotation mark “ This is equivalent to: echo ("\""); // Prints a quotation mark

String Manipulation Lesson 4 ord() ord() is chr()'s complement. It receives a character and returns the corresponding ASCII code as an integer. if ($c != ord(9) && $c != ord(13)) { // Only append $c if not tab or enter $a.= $c; }

String Manipulation Lesson 4 ord() ord() is chr()'s complement. It receives a character and returns the corresponding ASCII code as an integer. if ($c != ord(9) && $c != ord(13)) { // Only append $c if not tab or enter $a.= $c; }

String Manipulation Lesson 4 number_format() The function takes one, two, or four arguments (three arguments will result in An error). if only the first argument is used, num depicted as an integer with commas separating the thousands.

String Manipulation Lesson 4 number_format() if the first two arguments are used, the number will be shown with precision digits after the decimal point. The decimal point will be represented as a dot and commas will separate the thousands:

String Manipulation Lesson 4 number_format()

Lesson 4 Regular Expressions

Lesson 4 Regular Expressions is essentially a pattern, a set of characters that describes the nature of the string being Sought. The pattern can be a simple as a literal string, or it can be extremely complex. Using special characters to represent ranges of characters, multiple occurrences, or Specific contexts in which to search.

Regular Expressions Lesson 4 ^adam This pattern includes the special character ^, which indicates that the pattern should only match for strings that begin with the string “adam”, so the string “adam is the CEO” Would match this pattern, but string “ Mr.adam is the CEO” would not. Adam$ The $ character is used to match strings that end with the given pattern. Hello Mr.adam…….. match Hello Mr.adams…….. Doesn’t match

Lesson 4 Regular Expressions

Lesson 4 Regular Expressions

Lesson 4 ^adam This pattern includes the special character ^, which indicates that the pattern should only match for strings that begin with the string “adam”, so the string “adam is the CEO” Would match this pattern, but string “ Mr.adam is the CEO” would not. Adam$ The $ character is used to match strings that end with the given pattern. Hello Mr.adam…….. match Hello Mr.adams…….. Doesn’t match

Regular Expressions Lesson 4

Character Classes Lesson 4 In internet applications, regular expressions are especially useful for validating user input. You want to make sure that when a user submits a form, his or her phone number, address, credit card number, etc. all make reasonable sense. We need a way to describe the values that we are trying to match, and character classes provide a way to do that.

Character Classes Lesson 4 To create a character class that matches any one vowel, we place all vowels in square brackets.

Character Classes Lesson 4

Character Classes Lesson 4 [a-z] match any lowercase letter [A-Z] match any uppercase letter [a-zA-Z] match any letter [0-9] match any digit [0-9\.\-] match any digit, dot, or minus sign Be aware that each of these classes is used to match one character If you were attempting to match a string composed of one lowercase letter and one Digit only such as “a2”, ”t6”, “b1”, you could use ^[a-z][0-9]$

Character Classes Lesson 4

Character Classes Lesson 4 When “^” used inside the brackets of a character class, it means “not” or “exclude” ^[^0-9][0-9]$ The first character would be any non-digit character [^a-z] //Any character that is not a lowercase letter

Character Classes Lesson 4

Character Classes Lesson 4 [[:alpha:]] Any letter [[:digit:]] Any digit [[:alnum:]] Any letter or digit [[:space:]] Any white space [[:upper:]] Any uppercase letter [[:lower:]] Any lowercase [[:punct:]] Any punctuation mark

Character Classes Lesson 4

Character Classes Lesson 4 Detecting Multiple Occurrences We know now how to match a letter or digit, a word consists of one or more letters And a number consist of one or more digits, curly braces {} can be used to match Occurrence of the characters.

Character Classes Lesson 4 These examples demonstrate the three different usage of {}. With a single integer, {x} means “match exactly x occurrences of the previous character. with one integer and a comma, {x,} means “match x or more occurrences of the previous character”; And with two comma separated integers {x,y} means “match the previous character if it occures at least x times, but no more than y times”

Character Classes Lesson 4 Detecting Multiple Occurrences Character Class Description ^[a-zA-Z_]$ match any letter or underscore ^[[:alpha:]]{3}$ match any three letter word ^a$ match a ^a{4}$ match : aaaa ^a{2,4}$ match : aa or aaa or aaaa ^a{1,3}$ match a,aa, or aaa ^a{2,}$ match a string containing 2 or more a’s ^a{2,} match aasad,aaamad, but not apa a{2,} match saad,maaad but not bababa

Character Classes Lesson 4

Character Classes Lesson 4

Character Classes Lesson 4

Character Classes Lesson 4

Character Classes Lesson 4

Character Classes Lesson 4

Character Classes Lesson 4 ? Is equivalent to {0,1} + is equivalent to {1,} * is equivalent to {0,}.+ one or more if any character expect new line ^[a-zA-Z0-9_]+$ //match any word of at least one letter,number or _ ^[0-9]+$ //match any positive integer number ^\-?[0-9]+$ //match any integer number ^\-?[0-9]*\.?[0-9]*$ //match any double

Character Classes Lesson 4.+ one or more if any character expect new line Now lets validate address ^. \..+ $ Begin one or more one or more char. one or more char End

Character Classes Lesson 4

Character Classes Lesson 4 adams+ //matchs adams, adamss, adamssss…. (adam)+ // matchs adam,adamadam,adamadamadam ……… (Mad|Ad)am$ //matches Madam or Adam Am$ //matches the Am at the end of the string Jamal|adam$ //matches Jamal anywhere or adam at the end of the string (Jamaladam)$ //matches either Jamal or adam at the end