Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 9 Characters and Strings.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 6, 15) Recursive Data Structures.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
Intro to OOP with Java, C. Thomas Wu Characters and Strings
Fundamental Programming Structures in Java: Strings.
28-Jun-15 String and StringBuilder Part I: String.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 8 Characters and Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
1 Overview Regular expressions Notation Patterns Java support.
Characters and Strings. Representation of single characters Data type char is the data type that represents single characters, such as letters, numerals,
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,
Regular Language & Expressions. Regular Language A regular language is one that a finite state machine (fsm) will accept. ‘Alphabet’: {a, b} ‘Rules’:
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Last Updated March 2006 Slide 1 Regular Expressions.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9 Characters and Strings.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (adapted from.
Regular Expressions.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
17-Feb-16 String and StringBuilder Part I: String.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
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.
String and Lists Dr. José M. Reyes Álamo.
REGULAR EXPRESSION Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar.
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Chapter 3 : Basic Concept Of Java
University of Central Florida COP 3330 Object Oriented Programming
String String Builder.
Primitive Types Vs. Reference Types, Strings, Enumerations
JAVA RegEx Manish Shrivastava 11/11/2018.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Advanced String handling
String and StringBuilder
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Arrays and Collections
String and Lists Dr. José M. Reyes Álamo.
Microsoft Visual Basic 2005: Reloaded Second Edition
String and StringBuilder
Strings in Java Strings in Java are also a reference type.
Presentation transcript:

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 2 Useful String methods MethodMeaning compareToCompares the two strings lexicographically. int result = str1.compareTo( str2 ) substringExtracts the a substring from a string. String s2 = str1.substring( 1, 4 ) trimRemoves the leading and trailing spaces. str1.trim( ) concatConcatenates the specified string to the end of this string. String str3 = str1.concat( str2 ) equalsCompares the two strings (case-sensitive). boolean isSame = str1.equals( str2 ) equalsIgnoreCase Compares the two strings (case-insensitive). boolean isSame = str1.equalsIgnoreCase( str2 )

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 3 What if we want to identify specific characters in a String? MethodMeaning startsWith Returns true if a string starts with a specified prefix string. boolean hasPrefix = str1.startsWith( str2 ) endsWith Returns true if a string ends with a specified suffix string. boolean hasSuffix = str1.endsWith( str2 ) contains Returns true if a string contains the specified string. boolean hasContent = str1.contains( str2 )

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 4 More powerful methods MethodMeaning replaceAll Replaces all characters in the string that match the regular expression. String str3 = str1.replaceAll( regex, c ) matches Returns true if a string matches the specified regular expression. boolean matches = str1.matches( str2 )

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 5 Regular Expression Examples Regular Expression StringDescription [aeiou] A single character a, e, i, o, or u. [0-9][0-9] Any two-digit number from 00 to 99. [0-9&&[^4567]] A single digit that is 0, 1, 2, 3, 8, or 9. [a-z0-9] A single character that is either a lowercase letter or a digit. [a-zA-z][a-zA-Z0-9_$]* A valid Java identifier consisting of alphanumeric characters, underscores, and dollar signs, with the first character being an alphabetical character. [wb](ad|eed) Matches wad, weed, bad, and beed. (AZ|CA|CO)[0-9][0-9] Matches AZxx, CAxx, and COxx, where x is a single digit. See book pp

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 6 Rules of Regular Expressions The brackets [ ] represent choices An asterisk symbol * means zero or more occurrences. A plus symbol + means one or more occurrences. A hat symbol ^ means negation. A hyphen – means ranges. A parentheses ( ) and a vertical bar | mean a range of choices for multiple characters.

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 7 The matches() method uses Regular Expressions If str and regex are String objects: bool doesMatch = str.matches(regex);

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 8 The replaceAll() method replaces all occurrences of a substring that matches a given regular expression with a given replacement string. String originalText, modifiedText; originalText = “Hello there” modifiedText = // resulting string is Pattern: Replace all vowels with the

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 9 The Pattern and Matcher Classes The matches and replaceAll methods of the String class are shorthand for using the Pattern and Matcher classes from the java.util.regex package. The previous example: bool doesMatch = str.matches(regex); is equivalent to Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); bool doesMatch = m.matches();

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 10 What is the compile method? Converts the stated regular expression to an internal format to carry out the pattern-matching operation much more quickly. When using the matches method, the conversion is done every time. so it is more efficient to use the compile method when a search for the same pattern is needed multiple times.

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 11 The find Method The find method is another powerful method of the Matcher class. It searches for the next sequence in a string that matches the pattern, and returns true if the pattern is found. When a matcher finds a matching sequence of characters, we can query the location of the sequence by using the start and end methods.

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 12 Immutable Strings In Java a String object is immutable Once a String object is created, it cannot be changed, It is not possible to replace a character with another character, or to remove a character The String methods seen so far do not change the original string. A new string is created from the original. substring() creates a new string from a given string. The String class is defined in this manner for efficiency.

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 13 Effect of Immutability We can do this because String objects are immutable.

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 14 The StringBuilder Class In many string processing applications, it is necessary to change the contents of a string. i.e. it needs to be mutable The StringBuilder class is used for manipulating the contents of a string replacing a character, appending a string with another string, deleting a portion of a string

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 15 StringBuilder Example StringBuilder word = new StringBuilder("Java"); word.setCharAt(0, 'D'); word.setCharAt(1, 'i'); Changing a string Java to Diva word : StringBuilder Java Before word : StringBuilder Diva After word.setCharAt(0, 'D'); word.setCharAt(1, 'i');

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 16 Sample Processing Replace all vowels in the sentence with ‘X’. char letter; String inSentence = JOptionPane.showInputDialog(null, "Sentence:"); StringBuilder tempStringBuffer = new StringBuilder(inSentence); int numberOfCharacters = tempStringBuffer.length(); for (int index = 0; index < numberOfCharacters; index++) { letter = tempStringBuffer.charAt(index); if (letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i' || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U' ) { tempStringBuffer.setCharAt(index,'X'); } JOptionPane.showMessageDialog(null, tempStringBuffer );

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 17 append and insert methods append is used to append a String or StringBuilder object to the end of a StringBuilder object. The method can also take an argument of the primitive data type. Any primitive data type argument is converted to a string before it is appended to a StringBuilder object. A string can be inserted at a specified position by using the insert method.