1 9/27/05CS360 Windows Programming Strings, Regex, Web Response.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Regular Expressions using Ruby Assignment: Midterm Class: CPSC5135U – Programming Languages Teacher: Dr. Woolbright Student: James Bowman.
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
Regular Expression ASCII Converting. Regular Expression Regular Expression is a tool to check if a string matches some rules. It is a very complicated.
Asp.NET Core Vaidation Controls. Slide 2 ASP.NET Validation Controls (Introduction) The ASP.NET validation controls can be used to validate data on the.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Scripting Languages Chapter 8 More About Regular Expressions.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Applications of Regular Expressions BY— NIKHIL KUMAR KATTE 1.
Chapter 8: String Manipulation
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Last Updated March 2006 Slide 1 Regular Expressions.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
CS0004: Introduction to Programming Input and Output.
Lesson 7: Improving the User Interface
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
 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.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
C# Strings 1 CNS 3260 C#.NET Software Development.
Computer Programming 1.  Editor Console Application Notepad Notepad++ Edit plus etc.  Compiler & Interpreter Microsoft.NET Framework  Microsoft visual.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expression in Java 101 COMP204 Source: Sun tutorial, …
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
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.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
C# Strings 1 C# Regular Expressions CNS 3260 C#.NET Software Development.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CS360 Windows Programming
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
String Formatting Preparing strings for output. Printing println( arg ) prints its one argument on a line println can be used to print any single value,
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Regular expressions Day 11 LING Computational Linguistics Harry Howard Tulane University.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
Finding substrings my $sequence = "gatgcaggctcgctagcggct"; #Does this string contain a startcodon? if ($sequence =~ m/atg/) { print "Yes"; } else { print.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
Searching, Modifying, and Encoding Text. Parts: 1) Forming Regular Expressions 2) Encoding and Decoding.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
Introduction to Programming
28 Formatted Output.
Excel STDEV.S Function.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Data Types and Expressions
String Processing 1 MIS 3406 Department of MIS Fox School of Business
What We Want To Do User enters: Mary Smith
Presentation transcript:

1 9/27/05CS360 Windows Programming Strings, Regex, Web Response

2 9/27/05CS360 Windows Programming Last Time  We started talking about collections o Hash tables o ArrayLists

3 9/27/05CS360 Windows Programming Example  Let’s look at the example in Figure 3.3 on pages of the book

4 9/27/05CS360 Windows Programming Strings  Another way of finding the classes available through the.NET FCL is the Object Browser o View menu, then Object Browser  You will find the classes under mscorlib  String is under mscorlib->System o It’s in the System namespace

5 9/27/05CS360 Windows Programming Strings  The methods by the String class include: o ToLower o ToUpper o Replace o Length o IndexOf

6 9/27/05CS360 Windows Programming Example string theString = "Programming Microsoft.NET"; Console.WriteLine("default: \t {0}", theString); Console.WriteLine("lowercase: \t {0}", theString.ToLower()); Console.WriteLine("uppercase: \t {0}", theString.ToUpper()); Console.WriteLine("replace: \t {0}", theString.Replace(".", "dot ")); Console.WriteLine("length: \t {0}", theString.Length); Console.WriteLine("\"Microsoft\" occurs at character {0}", theString.IndexOf("Microsoft")); Console.WriteLine(); Console.WriteLine("Please press enter key to quit"); Console.ReadLine();

7 9/27/05CS360 Windows Programming What is the Output?  Strings can be combined using + or treated as character arrays string e = "dog" + "bee"; e += "cat"; string f = e.Substring(1,7); Console.WriteLine(f); for (int i = 0; i < f.Length; i++) { Console.Write("{0,-3}", f[i]); }

8 9/27/05CS360 Windows Programming Concatenation  The items being concatenated don’t have to be strings string t = "item " " sells at " + '\xA3' ; Console.WriteLine(t);

9 9/27/05CS360 Windows Programming Formatting Strings  Another useful class is String.Format, which has a lot in common with Console.WriteLine Console.WriteLine( "Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}", 123, 45.67, true, 'Q', 4, 5, 6, 7, '8'); string u = String.Format( "Hello {0} {1} {2} {3} {4} {5} {6} {7} {8}", 123, 45.67, true, 'Q', 4, 5, 6, 7, '8'); Console.WriteLine(u);

10 9/27/05CS360 Windows Programming Syntax for formatString  "{ N [, M ][: formatString ]}", arg1,... argN o N is a zero-based integer indicating the argument to be formatted o M is an optional integer indicating the width of the region to contain the formatted value, padded with spaces. If M is negative, the formatted value is left-justified; if M is positive, the value is right-justified o formatString is an optional string of formatting codes o argN is the expression to use at the equivalent position inside the quotes in the string

11 9/27/05CS360 Windows Programming Example of Using Width Region  What is the Output? Console.WriteLine( "\n{0,-10}{1,-3}", "Name","Salary"); Console.WriteLine( " "); Console.WriteLine( "{0,-10}{1,6}", "Bill", ); Console.WriteLine( "{0,-10}{1,6}", "Polly", 7890);

12 9/27/05CS360 Windows Programming Standard Format Specifiers CharacterInterpretation CCurrency DDecimal integer EExponent FFixed point GGeneral NSame as F with, every thousand PPercentage RRound trip XHex

13 9/27/05CS360 Windows Programming Example int i = ; Console.WriteLine("{0:C}", i); // $123, Console.WriteLine("{0:D}", i); // Console.WriteLine("{0:E}", i); // E+005 Console.WriteLine("{0:F}", i); // Console.WriteLine("{0:G}", i); // Console.WriteLine("{0:N}", i); // 123, Console.WriteLine("{0:P}", i); // 12,345, % Console.WriteLine("{0:X}", i); // 1E240

14 9/27/05CS360 Windows Programming Custom Format Specifiers 0Zero placeholderResults in a nonsignificant zero if a number has fewer digits than there are zeros in the format #Digit PlaceholderReplaces the pound symbol (#) with only significant digits.Decimal pointDisplays a period,Group SeparatorSeparates number groups, as in 1,000 %Percent NotationDisplay a percent E+0Exponent notationFormats the output of exponents \LiteralUsed as escape characters “abc”Literal stringDisplays what’s within quotes exactly ;Section separatorSpecifies different output if the numeric value to be formatted is positive, negative, or zero

15 9/27/05CS360 Windows Programming Example  Console.WriteLine("{0:#0;(#0); }", i);  Console.WriteLine("{0:#%}", i);  What is the output if: o i = o i = o i = 0

16 9/27/05CS360 Windows Programming Regular Expressions  System.Text provides a number of classes for regular expression processing  They are a powerful, flexible, and efficient strategy for processing text

17 9/27/05CS360 Windows Programming Regular Expressions.Matches any character except \n [characters]Matches a single character in a list [^characters]Matches a single character not in a list [charX-charY]Matches a single character in the specified range \wMatches a word character \WMatches a nonword character \sMatches a whitespace character \SMatches a nonwhitespace character \dMatches a decimal digit \DMatches a nondigit character

18 9/27/05CS360 Windows Programming Regular Expressions ^Bginning of the line $End of the line \bOn a word boundary \BNot on a word boundary *Zero or more matches +One or more matches ?Zero or one matches {n}Exactly n matches {n,}At least n matches {n,m}At least n but no more than m matches

19 9/27/05CS360 Windows Programming Regular Expressions ()Capture matched substring (? )Capture matched substring into group name |Logical OR

20 9/27/05CS360 Windows Programming Example  What is the output? string s = "Once Upon A Time In America"; Regex r = new Regex(" "); foreach (string ss in r.Split(s)) { Console.WriteLine(ss); }

21 9/27/05CS360 Windows Programming Multiple Delimiters string t = "Once,Upon:A/Time\\In\'America"; Regex q = new |,|:|/|\\|\'"); foreach (string ss in q.Split(t)) { Console.WriteLine(ss); }

22 9/27/05CS360 Windows Programming What ’ s the Output? string u = "Once Upon A Time In America"; Regex p = new Regex(" "); foreach (string ss in p.Split(u)) { Console.WriteLine(ss); }

23 9/27/05CS360 Windows Programming A Better Output string v = "Once Upon A Time In America"; Regex o = new foreach (string ss in o.Split(v)) { Console.WriteLine(ss); }  What if we are concerned with only spaces and not other whitespace characters?

24 9/27/05CS360 Windows Programming What ’ s Happening Here? string x = "Once Upon A Time In America"; Regex m = new Regex("( )"); foreach (string ss in m.Split(x)) { Console.WriteLine(ss); }

25 9/27/05CS360 Windows Programming Matching  Other classes in System.Text are Match and MatchCollection Regex r = new Regex("in"); Match m = r.Match("Matching"); if (m.Success) { Console.WriteLine( "Found '{0}' at position {1}", m.Value, m.Index); }

26 9/27/05CS360 Windows Programming Multiple Expressions Regex p = new Regex("((an)|(in)|(on))"); MatchCollection mn = p.Matches( "The King Kong Band Wins Again"); for (int i = 0; i < mn.Count; i++) { Console.WriteLine( "Found '{0}' at position {1}", mn[i].Value, mn[i].Index); }

27 9/27/05CS360 Windows Programming Backtracking Regex n = new Regex("Gr(a|e)y"); MatchCollection mp = n.Matches( "Green, Grey, Granite, Gray"); for (int i = 0; i < mp.Count; i++) { Console.WriteLine( "Found '{0}' at position {1}", mp[i].Value, mp[i].Index); }

28 9/27/05CS360 Windows Programming Groups Regex q = new Regex( "(? \\w+):(? \\w+)"); Match n = q.Match("Salary:123456"); Console.WriteLine( "{0} = {1}", n.Groups["something"].Value, n.Groups["another"].Value);  Output is: Salary =

29 9/27/05CS360 Windows Programming What is the Output? Console.WriteLine(); s first text loads of other stuff second text more third text "; e ]*href\s*=\s*[""']?([^'"">]+)['""]?>"; MatchCollection mc = Regex.Matches(s, e); foreach (Match mm in mc) Console.WriteLine("HTML links: {0}", mm);

30 9/27/05CS360 Windows Programming Breakdown of Expression ]*hrefMatch the character substring " " character, followed by the string "href", \s*=\s*followed by zero or more instances of whitespace, followed by the "=" character, followed by zero or more instances of whitespace, ['""]?followed by zero or one instance of single or double quotes, ([^'"">]+)followed by one or more instances of any characters except the single or double quotes or the closing ">", ['""]?>followed by zero or one instance of either the single or double quotes.

31 9/27/05CS360 Windows Programming Your Turn  Write a program that will convert a string to proper case o Initial caps on each word in the string  Sample output: o Initial String: the qUEEn wAs in HER parLOr o ProperCase: The Queen Was In Her Parlor

32 9/27/05CS360 Windows Programming Internet Classes  Let’s examine figure 3.5 on p. 82

33 9/27/05CS360 Windows Programming Summary  Collections o Strings o Regex o Internet Classes  Completed pages